Multithreaded form problem

J

Jeronimo Bertran

Hi,

I have a form with a pictureBox that should change from visible to not
visible depending on a status. The form receives events from a
communication thread in an object called commDriver.

In my form I register the event

commDriver.OnStatusReceived +=new StatusReceivedDelegate
(commDriver_OnStatusReceived);


Now, I am receiving the events just fine, I can also set the Text
property on the form's controls, however when I try to show or hide my
pictureBox, the application hangs.


I assumed that the problem had to do with having a different thread send
the event and not being able to update my control form a different
thread than the one used to create it.... so, I did the following:

private void commDriver_OnStatusReceived(object sender, string info)
{
if (myPicture.InvokeRequired)
{
// Update the status information asynchronously
UpdateStatusDelegate OnUpdateStatus = new
UpdateStatusDelegate(UpdateStatus);
OnUpdateStatus.BeginInvoke(info, null, null);
}
else
UpdateStatus(info);
}

And created the UpdateStatus method like this:

private void UpdateStatus(string info)
{
bool temp = myPicture.InvokeRequired;

// Do some work and....
// show or hide myPicture
myPicture.Visible = (info == "v");
}

For some reason the app still hangs when InvokeRequired returns true,
and I noticed that even though BeginInvoke is used,
myPicture.InvokeRequired STILL returns true.

Any ideas??

Thanks,

Jeronimo Bertran
 
J

Jon Skeet [C# MVP]

Jeronimo Bertran said:
I have a form with a pictureBox that should change from visible to not
visible depending on a status. The form receives events from a
communication thread in an object called commDriver.

In my form I register the event

commDriver.OnStatusReceived +=new StatusReceivedDelegate
(commDriver_OnStatusReceived);


Now, I am receiving the events just fine, I can also set the Text
property on the form's controls, however when I try to show or hide my
pictureBox, the application hangs.


I assumed that the problem had to do with having a different thread send
the event and not being able to update my control form a different
thread than the one used to create it.... so, I did the following:

private void commDriver_OnStatusReceived(object sender, string info)
{
if (myPicture.InvokeRequired)
{
// Update the status information asynchronously
UpdateStatusDelegate OnUpdateStatus = new
UpdateStatusDelegate(UpdateStatus);
OnUpdateStatus.BeginInvoke(info, null, null);
}
else
UpdateStatus(info);
}

That's calling BeingInvoke on a delegate, which ends up calling the
method on a threadpool thread, not the UI thread. You should use
myPicture.Invoke or myPicture.BeginInvoke instead.
 
J

Jeffrey Tan[MSFT]

Hi Jeronimo,

Thanks for your post!!

Yes, just as Jon pointed out, for multithreading in Winform, any worker
thread should use Control.Invoke or Control.BeginInvoke methods to marshal
the call to the UI side methods. This is documented in "Control.Invoke
Method" in MSDN:
Note There are four methods on a control that are safe to call from any
thread: Invoke, BeginInvoke, EndInvoke, and CreateGraphics. For all other
method calls, you should use one of the invoke methods to marshal the call
to the control's thread.

For delegate, we usually use asynchronized delegate(That is BeginInvoke
method of delegate) to leverage the Thread Pool threading processing, but
it does not provide the way to safe UI thread calling.

Also, there are a serial multithreading articles writen by Chris Sells, for
your information:
"Safe, Simple Multithreading in Windows Forms, Part 1"
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnforms/htm
l/winforms06112002.asp
==========================================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi Jeronimo,

I am glad my reply makes sense to you. If you need further help, please
feel free to post. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top