473,414 Members | 1,723 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 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 2666
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: BarryS | last post by:
Hi All: We're rolling out 3 DB2 (8.2 on LUW) platforms for branch offices, each of these are going to have the same schema. These are all going to be non-federated systems. I need some advice...
5
by: Mike Turco | last post by:
I've been upgrading since VB3 and Access 2. How long do I have to keep these CD's around as proof of valid licensing?
2
by: Jim Christiano | last post by:
I've created a C# window's form application that migrates data. As it migrates data, the program itself is non-responsive. I would like to implement a cancel button to stop the migration...
2
by: Jawahar | last post by:
All, I have an application in asp.net the application opens a lot of Modal diaglog pages. When modal dialogs are close the parent aspx page is often un-responsive. I mean I can click the buttons...
4
by: Urs Vogel | last post by:
Hi I would like to set a Windows.Form into a non responsive state, i.e. that it does not respond to user input of any kind, similar to the state a forms gets when another modal form is opened...
7
by: David Lozzi | last post by:
Howdy, I have a usercontrol in my aspx page and when the page loads, I send it some property values based on the data displayed. I am sending it to a public property like below. However the...
0
by: NoelByron | last post by:
I need to do some intense calculations in my application. Those are done by a third party Python library and consume 100% CPU time for some seconds. The computations take place in their own thread...
5
by: Radu | last post by:
Hi. I need to implement a "phone-book" kind of feature - I have a list of about 50.000 people, and the user should be able to find anyone by typing either the (beginning of the) last name, either...
4
by: Marina Levit | last post by:
I am trying to do some processing on a background thread while keeping the UI painting. However, this is a generic server side call routine - and it needs to block until the server side call...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.