Home All Groups Group Topic Archive Search About

Multithreading Invoke Workaround

Author
26 Nov 2007 7:36 AM
Joel Merk
When multithreading in my Windows form application, every time a non form
thread attempts to raise an event that modifies a control, I get an
exception. I understand that to avoid this, one way is to use the control's
Invoke method, with a delegate to the sub I would like to invoke. I was
wondering if there is any other way to avoid this problem, without having to
use an invoke for each event, because with a large number of events, it
starts to get confusing and relatively inefficient (if you ask me). I'd just
like to know if there's another way to raise events on the main form thread.
Thanks!

Author
26 Nov 2007 9:25 AM
Armin Zingler
"Joel Merk" <JoelM***@discussions.microsoft.com> schrieb
> When multithreading in my Windows form application, every time a non
> form thread attempts to raise an event that modifies a control, I
> get an exception. I understand that to avoid this, one way is to use
> the control's Invoke method, with a delegate to the sub I would like
> to invoke. I was wondering if there is any other way to avoid this
> problem, without having to use an invoke for each event, because
> with a large number of events, it starts to get confusing and
> relatively inefficient (if you ask me).

Efficiency at development time or run time? At run time (if you ask me)
efficiency is not that important because the user is usually the slowest
part in the chain. If there's a lot to update, either bulk updates to the UI
can be a solution, or don't putting the work in a background thread.

> I'd just like to know if
> there's another way to raise events on the main form thread. Thanks!

The only thing Invoke does is putting a message into the message queue of
the UI thread. That's the only way to make it work. Well, the "only" but
another one: Using a Timer to poll data from the worker threads.


Armin
Author
26 Nov 2007 3:40 PM
Spam Catcher
=?Utf-8?B?Sm9lbCBNZXJr?= <JoelM***@discussions.microsoft.com> wrote in
news:7835B1DE-7093-4B72-B6D5-B39C472A550E@microsoft.com:

> When multithreading in my Windows form application, every time a non
> form thread attempts to raise an event that modifies a control, I get
> an exception. I understand that to avoid this, one way is to use the
> control's Invoke method, with a delegate to the sub I would like to
> invoke. I was wondering if there is any other way to avoid this
> problem, without having to use an invoke for each event, because with
> a large number of events, it starts to get confusing and relatively
> inefficient (if you ask me). I'd just like to know if there's another
> way to raise events on the main form thread. Thanks!

You should use BeginInvoke instead of Invoke... because under certain
situations, Invoke can cause a deadlock.

If you're doing a lot of background updates ... perhaps create a helper
class or rearchitect your solution so that your code is more centralized?
Author
26 Nov 2007 4:23 PM
Brian Gideon
On Nov 26, 1:36 am, Joel Merk <JoelM***@discussions.microsoft.com>
wrote:
> When multithreading in my Windows form application, every time a non form
> thread attempts to raise an event that modifies a control, I get an
> exception. I understand that to avoid this, one way is to use the control's
> Invoke method, with a delegate to the sub I would like to invoke. I was
> wondering if there is any other way to avoid this problem, without having to
> use an invoke for each event, because with a large number of events, it
> starts to get confusing and relatively inefficient (if you ask me). I'd just
> like to know if there's another way to raise events on the main form thread.
> Thanks!

Consider having your UI thread poll a data structure for the objects
that represent an event.