本文介绍了调用时C#无效的跨线程调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!

我使用以下方法来调用某些内容:

Hello!

I use the following method to invoke something:

public void InvokeHelper(object from, object to)
        {
            BeginInvoke(new MethodInvoker(delegate()
            {
                from = to;
            }));
        }



但有时会抛出一个无效的crossthreadcall异常.我大约每秒使用50次.

所以问题是为什么它会引发异常?



But sometimes it throws an invalidcrossthreadcall exception. I use it about 50 times a second.

So the question is why is it throw exception?

推荐答案

public void InvokeHelper(object from, object to)
{
 BeginInvoke( (MethodInvoker) delegate { from = to; });
}



问候
Espen Harlinn



Regards
Espen Harlinn


if(this.InvokeRequired)
{
  this.BeginInvoke(method);
} else
{
  method();
}



http://msdn.microsoft.com/nl-nl/library/system.windows.forms.control.invokerequired.aspx [ ^ ]

http://msdn.microsoft.com/nl-nl/library/ms171728.aspx [ ^ ]

祝你好运!



http://msdn.microsoft.com/nl-nl/library/system.windows.forms.control.invokerequired.aspx[^]

http://msdn.microsoft.com/nl-nl/library/ms171728.aspx[^]

Good luck!


public void InvokeHelper(object from, object to, System.Threading.SynchronizationContext context)
        {

              _conex.Post(new SendOrPostCallback((obj) => {

                     //your code here
                     }
              ), null);

        }


这篇关于调用时C#无效的跨线程调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 21:00
查看更多