views:

74

answers:

2

Hi,
I have read that I can use asynchronous call with polling especially when the caller thread serves the GUI. I cannot see how because:

while(AsyncResult_.IsCompleted==false) //this stops the GUI thread
{
}

So how it come it should be good for this purpose? I needed to update my GUI status bar everytime deamon thread did some progress..

+1  A: 

You are correct in your while loop stopping the GUI thread, when doing it like that, you don't want to do that.

If you need to poll, it would be better is to set up a Timer, and check whether the work has completed when the timer fires. The Timer can have a small resolution without problems (100 ms for instance), as long as you dont do much work during each tick.

However, I think you would be even better off by using a callback, so you do not need to poll and get notified as soon as your workload is done.

driis
With callback I cannot let the main thread know (at least what I have read) - it is only when caller thread does not need to know reslts.
Tomas
@Tomas, the callback will execute on another thread, that is correct, but there is no problem in having it update your GUI (use Control.Invoke) or make the resulting data be available to the GUI thread.
driis
A: 

The point of async polling is that you can do other things in between checking IsCompleted — such as servicing GUI events. You could set a timer, for example, to trigger an event several times per second to check whether your asynchronous operation is finished, and use the normal GUI event loop to service those events together with all the other events your GUI receives. That way, your GUI remains responsive, and shortly after the async operation finishes, your timer event handler will notice it.

Wyzard
Thanks, I have thought of timer but as I am not experienced I thought it was a bad idea :)
Tomas