RunWorkerCompleted runs in worker thread

some problem with .net 2.0: In the application, a form subscribes a RunWorkerCompleted event of a BackgroundWorker. Suppose RunWorkerCompleted() is running in UI thread so some code inside it will do form rendering stuff. The application runs smooth in VS2005 environment, but it is running in worker thread when I run it in production environment (I tested it with InvokeRequired). The production environment has .Net 2.0 and .Net1.1 installed. In another production environment machine, .Net 2.0 and .Net1.1 are installed and this time the method is running in UI thread.

more investigation:
in production environment, the first call to RunWorkerCompleted(),
InvokeRequired returns false. But the second time to call
RunWorkerCompleted() with the same BackgroundWorker, the InvokeRequired
returns true.

so, what we should do is :

IF(This.InvokeRequired)

   this.Invoke(new Delegate(CallMethod),new object[]{arg1,arg2,xxx});

ELSE

   CallMethod(arg1,arg2,xxx);

原文地址:https://www.cnblogs.com/anorthwolf/p/1867372.html