Threading - Problem with Invoking between forms.

  • Thread starter Thread starter gwoodhouse
  • Start date Start date
G

gwoodhouse

Hello all,

I have an application at the moment which uses forms. The main form
(which has ALOT of things going on it in) also happens to do alot of
network proccess's which ive put in Threads.

DUring refactoring i decided i wanted to seperate as much non-form
related logical from the main page as i could. I created a new static
class, thinking i could call a "ThreadStart" method which, when it
needs to access form elements, drops into the main form through an
Invoke();

My problem is from my new class i can't use the Invoke() method. Ive
tried to solve this on my own and my theory is that the static class
doesnt know the Control that created it (because it wasnt created) so
therefor static methods can't use Invoke() ? ?

Could someone please tell me how to do something say...
mainThread.Invoke(mainThreadMethodHere)? (Or if im going about this
the wrong way - or if there is a better way - let me know?)

Thanks very much in advance!

Graeme
 
I have an application at the moment which uses forms. The main form
(which has ALOT of things going on it in) also happens to do alot of
network proccess's which ive put in Threads.

DUring refactoring i decided i wanted to seperate as much non-form
related logical from the main page as i could. I created a new static
class, thinking i could call a "ThreadStart" method which, when it
needs to access form elements, drops into the main form through an
Invoke();

My problem is from my new class i can't use the Invoke() method. Ive
tried to solve this on my own and my theory is that the static class
doesnt know the Control that created it (because it wasnt created) so
therefor static methods can't use Invoke() ? ?

Could someone please tell me how to do something say...
mainThread.Invoke(mainThreadMethodHere)? (Or if im going about this
the wrong way - or if there is a better way - let me know?)

Don't make the extra class static - make it a normal class, and pass a
reference to the creating control (i.e. "this") into the constructor,
so you can keep hold of it.

You can then call Invoke on that control to your heart's content.

For a decent level of separation, make the new class take an
ISynchronizeInvoke (which Control implements). That contains
InvokeRequired/Invoke/BeginInvoke/EndInvoke and means that in the
future you won't be tied to Windows Forms.
 
Hello Graeme,

I was having problems with this during development of a remote event
broadcasting app, but a consultant on the project solved it for us very
nicely.

Look into using the AsyncOperation and AsyncOperationManager classses
(CreateOperation(object userSuppliedState) method) to capture the correct
context to call back your UI thread. After we implemented his code, we no
longer needed to use Invoke() for each call to the form.

I found a code sample that he posted here ~
http://www.koders.com/csharp/fidC691B28DF08D8FCB61A78D36D11133F37BC4BA09.aspx
and, his code looks very similar to the C# sample here ~
http://msdn2.microsoft.com/en-us/library/system.componentmodel.asyncoperationmanager(VS.80).aspx


Good Luck - dave
 
Back
Top