471,603 Members | 1,503 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,603 software developers and data experts.

keeping UI responsive

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.

Dec 4 '05 #1
2 2592
<bl********@walla.co.il> wrote:
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.
Well, updating the UI is usually done using Control.Invoke or
Control.BeginInvoke, but starting the long-running thread can be done
in any number of ways.
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.


<snip>

I dare say there are ways of doing it, but it goes against the way that
Windows Forms (and Windows in general) was designed to work.

Could you explain why you don't want to just use a call-back, so that
when the long-running thread has finished doing something, a call is
made in the UI thread? What's the advantage of a separate, complicated
way of doing things?

See http://www.pobox.com/~skeet/csharp/t...winforms.shtml for more
information about threading in Windows Forms.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 4 '05 #2
Try Application.DoEvents()
<bl********@walla.co.il> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
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.

Dec 5 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by BarryS | last post: by
2 posts views Thread by Jim Christiano | last post: by
4 posts views Thread by Urs Vogel | last post: by
7 posts views Thread by David Lozzi | last post: by
reply views Thread by NoelByron | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by CCCYYYY | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.