PC Review


Reply
Thread Tools Rate Thread

Control.Invoke and anonymous methods

 
 
Lee Crabtree
Guest
Posts: n/a
 
      22nd Aug 2007
I'm a little confused about using Control.Invoke with an anonymous
method. It seems like the compiler is able to generate whatever glue
code is necessary to turn an anonymous method into a delegate type in
several situations, such as starting a thread:

Thread workerThread = new Thread(delegate { MessageBox.Show("Hello!"); });

However, trying to do the same thing with a form, as in:

Form1.Invoke(delegate { MessageBox.Show("Hello!"); });

nets an error stating that an anonymous method is not a delegate type,
and can't be converted. It's easy enough to wrap the anonymous method
in a MethodInvoker or whatever other required delegate type, but I don't
really understand why the difference exists at all.

Lee Crabtree
 
Reply With Quote
 
 
 
 
Patrick Steele
Guest
Posts: n/a
 
      26th Aug 2007
In article <eL$(E-Mail Removed)>, (E-Mail Removed)
says...
> I'm a little confused about using Control.Invoke with an anonymous
> method. It seems like the compiler is able to generate whatever glue
> code is necessary to turn an anonymous method into a delegate type in
> several situations, such as starting a thread:
>
> Thread workerThread = new Thread(delegate { MessageBox.Show("Hello!"); });
>
> However, trying to do the same thing with a form, as in:
>
> Form1.Invoke(delegate { MessageBox.Show("Hello!"); });
>
> nets an error stating that an anonymous method is not a delegate type,
> and can't be converted. It's easy enough to wrap the anonymous method
> in a MethodInvoker or whatever other required delegate type, but I don't
> really understand why the difference exists at all.


The Thread ctor accepts a specific type of delegate (ThreadStart). If
your anonymous method matches the signature of the delegate, the
compiler can do the work of inferring the type.

But Control.Invoke accepts any Delegate-derived type. The signature for
your anonymous method (void return type and no parameters) could match
any number of Delegate-derived types. Which one should the compiler
use? There's no way to infer that automatically.

--
Patrick Steele ((E-Mail Removed))
http://weblogs.asp.net/psteele
 
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
Trying to use anonymous methods to invoke code on a control =?Utf-8?B?QnJhZA==?= Microsoft Dot NET Compact Framework 1 4th Mar 2007 03:19 PM
Control access: Anonymous Methods, MethodInvoker _DD Microsoft C# .NET 4 25th May 2006 08:40 PM
Fun with anonymous methods :) William Stacey [MVP] Microsoft C# .NET 1 1st Mar 2006 07:08 PM
anonymous methods in c++/CLI bonk Microsoft VC .NET 2 14th Oct 2005 03:37 PM
invoke methods on the parent page from the user control Max Microsoft ASP .NET 3 24th Jan 2004 12:09 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:20 PM.