-1

我试图找到解决此问题的方法,但我没有找到或不知道,我是 C# 新手

我找到了很多谈论(调用)的解决方案,但我不知道如何在我的代码上修复它们,如果找到的一切都只是标签或文本框的解决方案,如果可能的话解决问题

“System.InvalidOperationException:'跨线程操作无效:控制''从创建它的线程以外的线程访问。'”

错误信息图片

            for (int i = 0; i <= len - 1; i++)
        {

            if (i % 3 == 0)
            {
                split = proxy[proxy_counter].Split(':');
                num.Rows.Add(numlist[i], 0, 0, split[0], split[1], split[2], split[3]);

                proxy_counter++;
            }
            else
            {
                num.Rows.Add(numlist[i], 0, 0, split[0], split[1], split[2], split[3]);
            }

        }
4

1 回答 1

0

问题出在MessageBox.Show. 您不能从后台线程更改 UI。为了做到这一点,您需要Invoke(如您所说)MessageBox.Show来自主要线程。

更改您的 MessageBox 行(假设那段代码在 Windows 窗体内):

InvokeIfRequired(() =>
    {
        MessageBox.Show("You Enter Less Than 6 Numbers!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    });

private void InvokeIfRequired(Action method)
{
    if (this.InvokeRequired)
    {
        Invoke(method);
    }
}
于 2018-12-15T09:54:38.860 回答