473,385 Members | 1,912 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,385 software developers and data experts.

Winforms threads UI not responding

Hi All,

I am using winform threads and I have craeted a new thread to perform
soem lengthy operations asynchronously.
My problem is that while this operation is being performed i am
updating the window status bar to display the status of the operation.
But as soon as the window loses the focus it freezes (mean the staus
bar is not updated anymore).

following is my code (in UI thread):

AsyncCallback ac = new AsyncCallback(ASyncCallBackMethod);
mi = new MethodInvoker(RunPackage);
mi.BeginInvoke(ac, null);

while((Helper.HelperInstance).Status != Helper.Success)
{
if(!InvokeRequired)
{
informationPanel.Text = "Now running: " +
(Helper.HelperInstance).NowRunning;
if(progressPanel.ProgressPosition == 50)
progressPanel.Reset();
progressPanel.Step();
Thread.Sleep(500);
}
}

progressPanel.Reset();
informationPanel.Text = "Operation Completed Successfully.";

ShowSummary(); //displays the results in a datagrid

FYI : when showSummary() is executed window is able to display the
final results.

Any help will be appreciated.
Thanks.

Vivek

May 22 '06 #1
8 13552
This is just snippets of your code, correct? What's your basic strategy
here in psuedo code? It looks to me like you're calling Thread.Sleep()
on the UI thread and that would be terrible. How is your UI thread
supposed to process messages?
vd*****@gmail.com wrote:
Hi All,

I am using winform threads and I have craeted a new thread to perform
soem lengthy operations asynchronously.
My problem is that while this operation is being performed i am
updating the window status bar to display the status of the operation.
But as soon as the window loses the focus it freezes (mean the staus
bar is not updated anymore).

following is my code (in UI thread):

AsyncCallback ac = new AsyncCallback(ASyncCallBackMethod);
mi = new MethodInvoker(RunPackage);
mi.BeginInvoke(ac, null);

while((Helper.HelperInstance).Status != Helper.Success)
{
if(!InvokeRequired)
{
informationPanel.Text = "Now running: " +
(Helper.HelperInstance).NowRunning;
if(progressPanel.ProgressPosition == 50)
progressPanel.Reset();
progressPanel.Step();
Thread.Sleep(500);
}
}

progressPanel.Reset();
informationPanel.Text = "Operation Completed Successfully.";

ShowSummary(); //displays the results in a datagrid

FYI : when showSummary() is executed window is able to display the
final results.

Any help will be appreciated.
Thanks.

Vivek


May 22 '06 #2
Yes I am calling thread.sleep in UI thread.
My UI thread is supposed to check for a variable
((Helper.HelperInstance).Status) after every 500 ms and update the UI.
When the Status == Success it proceeds and calls the ShowSummary() to
display the summary of results.
Is anything wrong in calling thread.sleep() on UI thread?

Thanks.

May 23 '06 #3
You NEVER want your UI thread suspended, that's the whole point of
doing the work asynchronously in the separate thread. What you really
want here is for your worker thread to call back to the UI thread and
say "I've completed this much work" and/or "I'm done". Meanwhile your
UI thread is free to process messages.

May 23 '06 #4
That's right. And thats what I am doing in my code. While loop is just
to update the UI Status Bar.
Now i've changed the code and am updating the UI on a separate thread
....here is the code..............

//main thread

AsyncCallback ac = new AsyncCallback(ASyncCallBackMethod);
mi = new MethodInvoker(RunPackage);
mi.BeginInvoke(ac, null);

Thread t = new Thread(new ThreadStart(UIUpdate));
t.Start();
Thread.CurrentThread.Join();

//new method which i added now
public delegate void UpdateStatusDelegate();

public void UIUpdate()
{
while((Helper.HelperInstance).Status != Helper.Success)
{
if(!InvokeRequired)
{
informationPanel.Text = "Now running: " +

(Helper.HelperInstance).NowRunning;
if(progressPanel.ProgressPosition == 50)
progressPanel.Reset();
progressPanel.Step();
return;
}
else
{
BeginInvoke(new UpdateStatusDelegate(UIUpdate));
Thread.Sleep(500);
}
}
}

But now nothing is happening. Not even a single update on UI.

Please advise.
Thanks.

May 23 '06 #5
Now what's the Thread.CurrentThread.Join() for? That doesn't make any
sense, you're saying "have this thread wait for itself to complete" so
it's obviously not going to complete. I really think you need to brush
up on .net Threading, there are plenty of good sources out there and it
would do you a lot of good.

http://www.yoda.arachsys.com/csharp/threads/

vd*****@gmail.com wrote:
That's right. And thats what I am doing in my code. While loop is just
to update the UI Status Bar.
Now i've changed the code and am updating the UI on a separate thread
...here is the code..............

//main thread

AsyncCallback ac = new AsyncCallback(ASyncCallBackMethod);
mi = new MethodInvoker(RunPackage);
mi.BeginInvoke(ac, null);

Thread t = new Thread(new ThreadStart(UIUpdate));
t.Start();
Thread.CurrentThread.Join();

//new method which i added now
public delegate void UpdateStatusDelegate();

public void UIUpdate()
{
while((Helper.HelperInstance).Status != Helper.Success)
{
if(!InvokeRequired)
{
informationPanel.Text = "Now running: " +

(Helper.HelperInstance).NowRunning;
if(progressPanel.ProgressPosition == 50)
progressPanel.Reset();
progressPanel.Step();
return;
}
else
{
BeginInvoke(new UpdateStatusDelegate(UIUpdate));
Thread.Sleep(500);
}
}
}

But now nothing is happening. Not even a single update on UI.

Please advise.
Thanks.


May 24 '06 #6
I agree becoz I am a newbie to threads....I put
Thread.CurrentThread.Join() there because I have created a new thread
't' which is going to keep updating the UI untill some condition
becomes true. And I want my main UI thread to keep waiting for the
thread t to finish its job before proceeding to some other task (thats
why Join(), its in the main threads execution block ). Am I doing
something wrong ?
Thanks for that link..I'll go through it.

Appreciate your help.

May 24 '06 #7
OK, lets take this back to basics.

If you UI thread is *waiting* for something, then the thread is not doing
anything. That would be the definition of "not responding".
I haven't read the entire chain, but it sounds like you actually need to
change the second thread to call back to the UI to tell it that it has
finished, so the UI thread can request the next thing. This can be done with
async-callbacks or events.

Doing background processing often takes more than changing one line of code;
for instance - the following (pseudocode) will *NOT* achieve the desired
result. It will hang the UI until everything has finished:

void SomeHandler(object sender, EventArgs args) {
foreach(Something s in thingsToDo) {
s.StartSomethingOnTheBackground();
s.WaitForCompletion();
}
}

Instead you would need to do something like (as an example; multiple
approaches exist) putting the "things to do" into a private Queue<T> on the
form, and then have something that kicks off the top item; when it is
notified of completion (callback or event) it checks for the next, and
starts that, etc. In between all of this, the UI is available for processing
messages such as "paint".

Does that help any?

Marc
May 24 '06 #8
Awesome !

That really helped. Really appreciate your help Marc.
Thanks a lot.

Vivek

May 24 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: mareal | last post by:
I am trying to write a basic load balancer (in our web service) solution. The purpose of this load balancer is to keep an array updated with server status. We have several servers that can be...
2
by: Steve Smith | last post by:
I have written an application that launches approximately 150 threads with Thread.ThreadStart() Each thread uses CDO 1.21 to logon to a different Exchange mailbox and send/receive a number of...
11
by: Steve Smith | last post by:
I have written a winforms application that launches approximately 150 threads with Thread.ThreadStart() Each thread uses CDO 1.21 to logon to a different Exchange mailbox and send/receive a...
17
by: OlafMeding | last post by:
Below are 2 files that isolate the problem. Note, both programs hang (stop responding) with hyper-threading turned on (a BIOS setting), but work as expected with hyper-threading turned off. ...
5
by: brian.wilson4 | last post by:
Our group is currently comparing winforms vs webforms.....app is Corp LAN based - we have control of desktops.....Below is pros and cons list we have come up with - if anything strikes you as...
5
by: Peted | last post by:
Just some threading questions 1. in a MDI app if you have multiple child forms, does each child form run in its own thread ? Eg do you get error trying to update one control on form1 from...
10
by: Jules Winfield | last post by:
Guys, I've been designing applications for the financial services industry using MSFT technologies for many years (Win32 API, MFC, and now .NET WinForms). All applications are Internet-based,...
3
by: =?Utf-8?B?Y2hlbmRyaWNrcw==?= | last post by:
I have a C# winforms application that makes periodic web service calls in background thread to my web service server. These calls work fine almost all the time but on rare occassions the web...
4
by: =?Utf-8?B?cmFuZHkxMjAw?= | last post by:
Visual Studio 2005, C# WinForms application: Here’s the question: How can I increase the standard 1 MB stack size of the UI thread in a C# WinForms application? Here’s why I ask: I’ve...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...

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.