Worker Thread raising events to the main thread?

O

ORC

Is it possible to have a worker thread raising events to the main thread so
that the main thread can update the form controls etc.? Let's say that the
worker thread is a loop that randomly reads from a file. After every loop it
saves the readen value in a buffer and raises an event for the main thread
that then reads the buffer and updates the form - (the worker thread is
still running the loop) - How to do this if possible???

Thanks
Ole
 
D

Daniel Moth

Some options are:
1. Don't worry about blocking the worker thread and update the GUI controls
(via Control.Invoke)
2. Run yet another worker thread that just reads from the buffer and update
the GUI (via Control.Invoke of course)
3. From your original worker thread use ThreadPool.QueueUserWorkItem and in
the WaitCallback handler update the GUI (always via Control.Invoke)

The code in the following posts may be of use to you:
http://www.danielmoth.com/Blog/2004/11/dont-poll.html
http://www.danielmoth.com/Blog/2004/10/invoke-cf-and-full-fx.html

Cheers
Daniel
 
O

ORC

Thanks for your propt reply!

There's no problem in blocking the worker thread while updating the controls
so solution no. 1 will suite me. But do I have to Control.Invoke every
single control on the form or is it possible to have a function that updates
all controls in one step (line by line offcouse :) ?

Thanks
Ole
 
D

Daniel Moth

Control.Invoke will point to one of your own methods; in there you can touch
GUI controls as much as you want. Again, sample code on my blog.

Cheers
Daniel
 
O

ORC

Great - thanks!

Ole

Daniel Moth said:
Control.Invoke will point to one of your own methods; in there you can touch
GUI controls as much as you want. Again, sample code on my blog.

Cheers
Daniel
 

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