Winforms threads UI not responding

V

vdahiya

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
 
S

sdbillsfan

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?
 
V

vdahiya

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.
 
S

sdbillsfan

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.
 
V

vdahiya

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.
 
S

sdbillsfan

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/
 
V

vdahiya

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.
 
M

Marc Gravell

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
 
V

vdahiya

Awesome !

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

Vivek
 
Top