Reporting progress from worker thread

  • Thread starter Thread starter Tomaz Koritnik
  • Start date Start date
T

Tomaz Koritnik

I have a worker thread doing some long calculation and I'd like to report
progress (percent done,...). One easy way it to call Control.Invoke(). But
is there another way of doing this without using controls that are
associated with window handles?

regards
Tomaz
 
Tomaz said:
I have a worker thread doing some long calculation and I'd like to
report progress (percent done,...). One easy way it to call
Control.Invoke(). But is there another way of doing this without
using controls that are associated with window handles?

regards
Tomaz

There are a variety of ways. One simple way could be to update stats in an
object that both threads have access to whenever it's convienient for the
worker thread to do so. The foreground thread could then display results
(by reading stats from that object) to the user on a timed basis (update the
UI once every N seconds, etc.). You'd want to control access to
updating/reading that object of course.
 
Tomaz,

If you want to have some sort of notification that is visual, then
ultimately, at some level, you will have to call the Invoke method to make
the call to update the UI.

You can have your component fire events to indicate progress, however,
the handlers will have to eventually call Invoke because the event being
fired is not on the UI.

If you check out Juval Lowy's book, "Programming .NET Components", there
is a section on events and multithreading, along with a utility class
(EventsHelper) which will take an implementation of ISynchronizeInvoke and
perform the event firing on the appropriate thread.

Hope this helps.
 
Hi

This pull-model is just what I thought about too and it's a great idea. Stat
object would use critical section to support multithreading and main thread
would get data from info object using a timer with speed of 1Hz.

regards
Tomaz
 
Hi

Thanks for answering. Is there a simple way to use messages (like in Win32)
for communication? I will also check the Juval Lowy's book because I found
it very interesting.

regards
Tomaz


Nicholas Paldino said:
Tomaz,

If you want to have some sort of notification that is visual, then
ultimately, at some level, you will have to call the Invoke method to make
the call to update the UI.

You can have your component fire events to indicate progress, however,
the handlers will have to eventually call Invoke because the event being
fired is not on the UI.

If you check out Juval Lowy's book, "Programming .NET Components",
there is a section on events and multithreading, along with a utility
class (EventsHelper) which will take an implementation of
ISynchronizeInvoke and perform the event firing on the appropriate thread.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Tomaz Koritnik said:
I have a worker thread doing some long calculation and I'd like to report
progress (percent done,...). One easy way it to call Control.Invoke(). But
is there another way of doing this without using controls that are
associated with window handles?

regards
Tomaz
 
Tomaz,

Why would you want to use messages? It's much easier to just declare a
delegate type and then attach the method you want to call to it, and pass it
(with any parameters) to the Invoke method.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Tomaz Koritnik said:
Hi

Thanks for answering. Is there a simple way to use messages (like in
Win32) for communication? I will also check the Juval Lowy's book because
I found it very interesting.

regards
Tomaz


Nicholas Paldino said:
Tomaz,

If you want to have some sort of notification that is visual, then
ultimately, at some level, you will have to call the Invoke method to
make the call to update the UI.

You can have your component fire events to indicate progress, however,
the handlers will have to eventually call Invoke because the event being
fired is not on the UI.

If you check out Juval Lowy's book, "Programming .NET Components",
there is a section on events and multithreading, along with a utility
class (EventsHelper) which will take an implementation of
ISynchronizeInvoke and perform the event firing on the appropriate
thread.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Tomaz Koritnik said:
I have a worker thread doing some long calculation and I'd like to report
progress (percent done,...). One easy way it to call Control.Invoke().
But is there another way of doing this without using controls that are
associated with window handles?

regards
Tomaz
 

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

Back
Top