The Artima Developer Community
Sponsored Link

.NET Buzz Forum
UISafeInvoker: Making UI Thread safe methods - a little more easily

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Roy Osherove

Posts: 1807
Nickname: royo
Registered: Sep, 2003

Roy Osherove is a .Net consultant based in Israel
UISafeInvoker: Making UI Thread safe methods - a little more easily Posted: May 23, 2005 5:08 PM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Roy Osherove.
Original Post: UISafeInvoker: Making UI Thread safe methods - a little more easily
Feed Title: ISerializable
Feed URL: http://www.asp.net/err404.htm?aspxerrorpath=/rosherove/Rss.aspx
Feed Description: Roy Osherove's persistent thoughts
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Roy Osherove
Latest Posts From ISerializable

Advertisement
Here's something I've been using in my programs lately. I've been pretty darn annoyed with the whole threading issue in Winforms - that is - lack of property semantics to take care of it. So, I created a hack, It's a little ugly, but it lets me write much less code to make my GUI methods in forms thread safe (UI thread safe, I mean).
The main problem is that when you invoke a method on a form that deals with the GUI on that form (changes text or does something to a visual property of a control) it may work and it may not, mostly not. It may hang in a weird way or any million other weird things might happen. In fact, in .NET 2.0 you'll be getting an *exception* if a thread other that the UI thread will be invoking some UI action, so this needs to be taken care of.
Here's how you take care of it today:
 
Assume the method:
 
private void EnableButtons(bool enabled)
{
   cmdDoSomething.Enabled = enabled;
   cmdStop.Enabled = !enabled;
}
 
If you get a non GUI thread to run this method your application might hang and die. So what you do is this:
 
private delegate void enableButtonsDelegate(bool enabled);
private void EnableButtons(bool enabled)
{
   if(InvokeRequired)
   {
      Invoke(new enableButtonsDelegate(EnableButtons),enabled);
         return;
   }
 
   cmdDoSomething.Enabled = enabled;
   cmdStop.Enabled = !enabled;
}
 
that's 4 ugly lines of code you have to write. And you need to create such a delegate for each special method signature you might want to make thread safe (the alternative is to have a standard on thread safe methods and use the same delegate type each time, but that also is pretty ugly and hard to maintain.
 
So, here's my way:
 
You download my Safe Invoker class from this location, and then go on to write the method code like this:
 
private void EnableButtons(bool enabled)
{
if(Safe.Invoke(this,enabled)
   return;
 
cmdDoSomething.Enabled = enabled;
cmdStop.Enabled = !enabled;
}
 
Done.
 
Here are some other use cases for this:
 
public void ThreadSafeEmptyMethod()
 {
  if(Safe.Invoke(this))
   return;
   
  //do whatever GUI things you want here!
 }
 
 public void ThreadSafeMethodWithParams(int i, string str)
 {
  if(Safe.Invoke(this,i,str))
   return;
   
  //do whatever GUI things you want here!
 }
 
 public int ThreadSafeMethodWithParamsAndReturnValue(int i, string str)
 {
  if(InvokeRequired)
   return Safe.InvokeAndReturn(this,i,str);
 
      
  //do whatever GUI things you want here!
  TextBox1.Text = "whatever";
  return 1;
 }

 
Under the covers there is a little reflection going on a little ugly trick in the Safe class, but the main thing to know is that when you're dealing with a different thread, performance is not really the issue here anyway, plus, it works great and you don't really notice anything in the UI (I should know - the new Regulator 2005 is using this stuff all over).
 
The class itself is pretty small (the solution is quite simple), but I have a whole other story about making this work *the hard way* which I'll tell when I get more time 9you *did notice I have much less time than I used to, right?;) )
 

Read: UISafeInvoker: Making UI Thread safe methods - a little more easily

Topic: [TechEd Europe 2005] INETA BoF Sessions to be selected soon Previous Topic   Next Topic Topic: Review: Follow-Up Notes on ReSharper and .NET Refactor

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use