0

我遇到了以下问题:我有一个配置文件和一个对此文件的侦听器。更改后,会触发一个方法 ( createMenuAndItems),该方法会修改toolStripMenuItem.

该方法createMenuAndItems在加载表单和修改配置文件时调用。每当数组

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

有多个项目,当我修改配置文件时显示错误。在加载表单时工作正常。

Visual Studio 中显示的错误:

System.InvalidOperationException 未处理

HResult=-2146233079
消息=:跨线程操作无效:控件“topMenu”从创建它的线程以外的线程访问

实际消息是

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

源 = System.Windows.Forms
StackTrace:

试过CheckForIllegalCrossThreadCalls = False;但没有运气

奇怪的是,如果数组strArrays只有一个值,它就像一个魅力。但是当添加更多项目时开始显示此错误

这是代码。

文件上的侦听器:

    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;
    }

这是创建和修改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);
                }
            }
        }
    }
}

谢谢!

4

1 回答 1

1

最后通过内部的主线程调用菜单和项目的创建解决了这个问题configWatcher()

像这样:

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

代替:

fileSystemWatcher.Changed += new FileSystemEventHandler(this.createMenuAndItems);
于 2017-07-20T19:08:19.203 回答