Multithreading Invoke Workaround

J

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!
 
A

Armin Zingler

Joel Merk said:
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
 
G

Guest

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?
 
B

Brian Gideon

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top