PC Review


Reply
Thread Tools Rate Thread

Multithreaded form problem

 
 
Jeronimo Bertran
Guest
Posts: n/a
 
      15th Apr 2005
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

 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      15th Apr 2005
Jeronimo Bertran <(E-Mail Removed)> wrote:
> 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.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
 
 
 
Jeffrey Tan[MSFT]
Guest
Posts: n/a
 
      15th Apr 2005
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/de...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.

 
Reply With Quote
 
Jeronimo Bertran
Guest
Posts: n/a
 
      18th Apr 2005
Thanks Jeffrey, both posts were very helpful.
 
Reply With Quote
 
Jeffrey Tan[MSFT]
Guest
Posts: n/a
 
      19th Apr 2005
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
modeless form from a multithreaded application Richard Bell Microsoft VB .NET 4 3rd Mar 2004 11:43 PM
MultiThreaded Form Ken Saganowski Microsoft C# .NET 5 3rd Feb 2004 10:06 PM
MultiThreaded form refresh Ken Saganowski Microsoft C# .NET 2 5th Jan 2004 08:13 PM
Multithreaded vs Multithreaded DLL Pradyumna Microsoft Dot NET Framework 2 22nd Oct 2003 01:42 PM
Multithreaded vs Multithreaded DLL pradyumna Microsoft Dot NET 2 22nd Oct 2003 10:40 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:02 PM.