Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C中跨线程调用函数#_C#_.net_Multithreading_Winforms - Fatal编程技术网

C# C中跨线程调用函数#

C# C中跨线程调用函数#,c#,.net,multithreading,winforms,C#,.net,Multithreading,Winforms,我在WindowsForms中有一个称为grid1的网格 我知道要从另一个线程调用grid1.Series.Clear() 现在我得到了一个常见的跨线程异常,一个线程正在接触grid1,但另一个线程创建了它 我找到了很多如何调用grid1的直接函数的例子 但是如何从grid1中的SeriesCollection“Series”调用Clear()函数?此代码在创建grid1控件的线程以外的线程上运行 grid1.Invoke((MethodInvoker)(() => grid1.Serie

我在WindowsForms中有一个称为
grid1
的网格

我知道要从另一个线程调用
grid1.Series.Clear()

现在我得到了一个常见的跨线程异常,一个线程正在接触grid1,但另一个线程创建了它

我找到了很多如何调用grid1的直接函数的例子


但是如何从grid1中的SeriesCollection“Series”调用
Clear()
函数?

此代码在创建grid1控件的线程以外的线程上运行

grid1.Invoke((MethodInvoker)(() => grid1.Series.Clear()));

如下所示:

grid1.BeginInvoke(new MethodInvoker(() => grid1.Series.Clear()));
也许这会有帮助

如果你想为它创建一个函数,这是一个例子

delegate void FunctionNameCallBack(InputParams);
private void FunctionName(InputParams)
{
    if (this.InvokeRequired)
    {
        var d = new FunctionNameCallBack(FunctionName);
        this.Invoke(d, InputParams);
    }
    else
    {
        // Your Code here.
    }
}

这个答案怎么了?