CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Jeremy D. Miller -- The Shade Tree Developer

Under the hood and working with .Net, TDD, Software Design, and Agile Stuff

Using Anonymous Methods with Control.Invoke()

 

Just in case I'm the only person that didn't know this, you can't pass an anonymous method as an argument to Control.Invoke() when you're trying to make cross-thread calls.  Instead, you have to wrap it in a Delegate like the MethodInvoker delegate like this code below:

 

   23         public string CurrentActivity

   24         {

   25             get { return _activityStrip.Text; }

   26             set

   27             {

   28                 if (InvokeRequired)

   29                 {

   30                     MethodInvoker invoker = new MethodInvoker(delegate()

   31                                                                   {

   32                                                                       _activityStrip.Text = value;

   33                                                                   });

   34                     Invoke(invoker);

   35                 }

   36                 else

   37                 {

   38                     _activityStrip.Text = value;

   39                 }

   40             }

   41         }

It's fine and it works, but it's butt ugly.  I'm mildly tempted to try out Boo for this very reason or maybe play with RubyCLR to drive WinForms with a cleaner syntax.



Comments

David M. Kean said:

Or why don't you add an overload to your base Form that takes a MethodInvoker:

public void Invoke(MethodInvoker invoker)

{

           Invoke((Delegate)invoker);

}

Now you can call Invoke with an anonymous method.

# November 6, 2006 8:50 PM

Bhavesh said:

I still think is has performance issues.

I have taken an example to update progressbar while reading a huge file. If I update in unsafe way in the thread, the program executes amazingly fast.

MethodInvoker myCallback = new MethodInvoker(

               delegate()

{

progressBar1.Value = m_iPercentage;

});

//progressbar percentage

Double perRead = (Convert.ToDouble(fStream.Position) / Convert.ToDouble(fStream.Length)) * 100.0;

m_iPercentage = Convert.ToInt32(perRead);

progressBar1.Value = m_iPercentage;

//Invoke(myCallback);

as soon as i comment progressbar1.value = m_iPercentage; and run through Invoke(myCallback); the program runs hell lot slower.

Do know what to do here.

# December 5, 2006 4:56 AM

Myrddin said:

Try using BeginInvoke instead so your thread doesn't block waiting for the target thread's message loop.

# December 13, 2006 4:12 AM

Myrddin said:

Also, you really should keep track of your last percentage and avoid doing the invoke all together unless the value has changed.  Invokes by their very nature are slow and even if you don't block on them it'll be better for your application if you don't invoke unless you absolutely have to.

# December 13, 2006 4:16 AM

Jonathon Rossi said:

Calling Control.Invoke() with anonymous methods

# March 2, 2008 7:21 AM

Dana Hanna said:

In 3.5, you could create a simple extension method on the Control object with this overload which will allow a anony delegate to be passed to invoke:

public static void Invoke(this Control c, MethodInvoker mi)

{

   c.Invoke((Delegate)mi);

}

# May 20, 2008 2:34 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Jeremy D. Miller

Jeremy began his IT career writing "Shadow IT" applications to automate his engineering documentation, then wandered into software development because it looked like more fun. Jeremy previously worked as a systems architect building mission critical supply chain software for a Fortune 100 company and learned agile development practices as a .Net consultant at ThoughtWorks, one of the pioneers of agile development. Jeremy is the author of the open source StructureMap (http://structuremap.sourceforge.net) tool for Dependency Injection with .Net and the forthcoming StoryTeller (http://storyteller.tigris.org) tool for supercharged FIT testing in .Net. Jeremy's thoughts on just about everything software related can be found on his weblog "The Shade Tree Developer" at http://codebetter.com/blogs/jeremy.miller, part of the popular CodeBetter site. Jeremy is a Microsoft MVP for C#. Check out Devlicio.us!

Our Sponsors

Free Tech Publications

This Blog

Syndication

News

All opinions expressed here constitute my (Jeremy D. Miller's) personal opinion, and do not necessarily represent the opinion of any other organization or person, including (but not limited to) my fellow employees, my employer, its clients or their agents.

About Me

"Best Of" Compendium

StructureMap (Dependency Injection for .Net)

StoryTeller (Supercharged Fit)

Build your own Cab

TestDriven

MVP