By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,920 Members | 1,660 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,920 IT Pros & Developers. It's quick & easy.

Threading - Problem with Invoking between forms.

P: n/a
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

May 9 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
gw********@gmail.com <gw********@gmail.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 9 '07 #2

P: n/a
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/fidC691...37BC4BA09.aspx
and, his code looks very similar to the C# sample here ~
http://msdn2.microsoft.com/en-us/lib...er(VS.80).aspx
Good Luck - dave
<gw********@gmail.comwrote in message
news:11**********************@e51g2000hsg.googlegr oups.com...
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

May 9 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.