Error while dynamically creating toolStripMenuItem

216 Views Asked by At

I'm having the following issue: I have a config file and a listener over this file. When changed, triggers a method (createMenuAndItems), that modifies the toolStripMenuItem.

The method createMenuAndItems is called upon load of the Form and when the config file is modified. Whenever the array

strArrays = new string[] { "Copy File" ,  "Exit"};

has more than one item, shows an error when I modify the config file. on loading the Form works fine.

The error as shown in Visual Studio:

System.InvalidOperationException was unhandled

HResult=-2146233079
Message=: Cross-thread operation not valid: Control 'topMenu' accessed from a thread other than the thread it was created on

The actual message is

Message=Operación no válida a través de subprocesos: Se tuvo acceso al control 'topMenu' desde un subproceso distinto a aquel en que lo creó.

Source=System.Windows.Forms
StackTrace:

Tried with CheckForIllegalCrossThreadCalls = False; but no luck

The strange thing is that if the array strArrays has just one value, it works like a charm. but when adding more items starts showing this error

Here's the code.

The listener over the file:

    private void configWatcher()
    {
        FileSystemWatcher fileSystemWatcher = new FileSystemWatcher()
        {
            Path = string.Concat(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "\\Launcher"),
            Filter = "Config.xml"
        };
        fileSystemWatcher.Changed += new FileSystemEventHandler(this.createMenuAndItems);
        fileSystemWatcher.EnableRaisingEvents = true;
    }

Here's the code that creates and modifies the toolStripMenuItem

private void createMenuAndItems()
{
    string str;
    ToolStripMenuItem toolStripMenuItem;
    int i;
    base.Invoke(new Action(() =>
    {
        base.Controls.Find("topMenu", false);
        if (base.Controls.IndexOfKey("topMenu") > 0)
        {
            base.Controls.RemoveByKey("topMenu");
        }
    }));
    MenuStrip menuStrip = new MenuStrip()
    {
        Name = "topMenu"
    };
    base.Invoke(new Action(() => this.Controls.Add(menuStrip)));
    string[] strArrays = new string[] { "Menú" };
    string[] array = strArrays;
    for (i = 0; i < (int)array.Length; i++)
    {
        ToolStripMenuItem toolStripMenuItem1 = new ToolStripMenuItem(array[i]);
        menuStrip.Items.Add(toolStripMenuItem1);
    }
    foreach (ToolStripMenuItem item in menuStrip.Items)
    {
        if (item.Text == "Menú")
        {
            strArrays = new string[] { "Copy File" ,  "Exit"};
            array = strArrays;                    
            for (i = 0; i < (int)array.Length; i++)
            {
                str = array[i];
                toolStripMenuItem = new ToolStripMenuItem(str, null, new EventHandler(this.ChildClick));
                item.DropDownItems.Add(toolStripMenuItem);
            }
            foreach (ToolStripMenuItem dropDownItem in item.DropDownItems)
            {
                if (dropDownItem.Text == "Copy File")
                {
                    //gives a list of strings.
                    array = this.fetchServersFromConfig().ToArray();
                    for (i = 0; i < (int)array.Length; i++)
                    {
                        str = array[i];
                        toolStripMenuItem = new ToolStripMenuItem(string.Concat("Origin: ", str), null, new EventHandler(this.copyFile));
                        dropDownItem.DropDownItems.Add(toolStripMenuItem);
                    }
                }
                if (dropDownItem.Text == "Exit")
                {                            
                    dropDownItem.ShortcutKeys = Keys.Control | Keys.Q;
                    dropDownItem.Click += new EventHandler(this.exitButtonMenu);
                }
            }
        }
    }
}

Thanks!

1

There are 1 best solutions below

0
On

Finally solved this by invoking the Creation of Menu and Items through the main thread inside the configWatcher()

Like this:

fileSystemWatcher.Changed += new FileSystemEventHandler(base.Invoke(new Action(() => this.createMenuAndItems())););

Instead of:

fileSystemWatcher.Changed += new FileSystemEventHandler(this.createMenuAndItems);