B
blueturtle
Hi,
I'm newbie to C#, and I would like to know what is the common solution
to a problem that I encounter.
The scenario:
Performing a long task, without blocking the UI thread, so it will stay
responsive.
The long task is done in a different thread.
According to want I've read, in C# it is performed using IAsyncResult
and BeginInvoke.
This solves the issue of blocking, however it requires me to use
delegates to receive the result of the task,
so the code becomes "event driven".
I'm looking for something else: do the task in different thread, wait
for event and keep UI responsive, all in one time.
So the code of UI will treat the method calls, as if they are
synchronous.
This can be achieved in C++ by "Wait and keep pumping messages":
pseudo:
while (true)
{
while (PeekMessage(...)
DispatchMessage(...);
MsgWaitForMultipleObjects(...);
}
I've read somewhere the WaitOne() is implemented using
MsgWaitForMultipleObjects, so I've tried:
ar = d.BeginInvoke(params, new AsyncCallback(MyCallback) ,null);
ar.AsyncWaitHandle.WaitOne();
but it makes the UI very unresponsive (painting and mouse),
I guess that's because I cannot control the filter of the messages.
How can it be done in C# ?
Am I doing something wrong ?
Thanks for any help,
Si.
I'm newbie to C#, and I would like to know what is the common solution
to a problem that I encounter.
The scenario:
Performing a long task, without blocking the UI thread, so it will stay
responsive.
The long task is done in a different thread.
According to want I've read, in C# it is performed using IAsyncResult
and BeginInvoke.
This solves the issue of blocking, however it requires me to use
delegates to receive the result of the task,
so the code becomes "event driven".
I'm looking for something else: do the task in different thread, wait
for event and keep UI responsive, all in one time.
So the code of UI will treat the method calls, as if they are
synchronous.
This can be achieved in C++ by "Wait and keep pumping messages":
pseudo:
while (true)
{
while (PeekMessage(...)
DispatchMessage(...);
MsgWaitForMultipleObjects(...);
}
I've read somewhere the WaitOne() is implemented using
MsgWaitForMultipleObjects, so I've tried:
ar = d.BeginInvoke(params, new AsyncCallback(MyCallback) ,null);
ar.AsyncWaitHandle.WaitOne();
but it makes the UI very unresponsive (painting and mouse),
I guess that's because I cannot control the filter of the messages.
How can it be done in C# ?
Am I doing something wrong ?
Thanks for any help,
Si.