Anonymous Delegate

S

Stefan Hoffmann

hi @all,

is there something like an anonymous delegate?

This is the original code:

--
private delegate void DelegateSetFormCaption(string text);
private void SetFormCaption(string text)
{
if (this.InvokeRequired)
{
DelegateSetFormCaption d = new
DelegateSetFormCaption(SetFormCaption);
this.Invoke(d, new object[] { text });
}
else { this.Text = text; }
}
--

I'd like to avoid the delegate declaration, e.g.:

--
private void SetFormCaption(string text)
{
if (this.InvokeRequired)
{
this.Invoke(
new delegate(string text)(SetFormCaption), new object[] { text }
);
}
else { this.Text = text; }
}
--

Is this possible?


mfG
--> stefan <--
 
B

Barry Kelly

Stefan said:
private void SetFormCaption(string text)
{
if (this.InvokeRequired)
{
this.Invoke(
new delegate(string text)(SetFormCaption), new object[] { text }
);
}
else { this.Text = text; }
}

If you're using C# 3.0:

this.Invoke((Action) () => this.Text = text);

Action delegate type is in the System namespace in System.Core assembly.

If you're using C# 2.0:

delegate void Action(); // a single delegate type will do for all
// ...
this.Invoke((Action) delegate { this.Text = text; });

The reference to 'text' in the C# 3.0 syntax (lambda syntax) and in the
C# 2.0 anonymous delegate syntax is ok, because the parameter will be
captured and thus made available to the delegate when the control
invokes it on the UI thread.

-- Barry
 
B

Ben Voigt [C++ MVP]

If you're using C# 2.0:
delegate void Action(); // a single delegate type will do for all
// ...
this.Invoke((Action) delegate { this.Text = text; });

Use MethodInvoker instead of defining your own. The CLR recognizes
MethodInvoker and executes a specialized fast-path implementation of Invoke.
 
B

Ben Voigt [C++ MVP]

Peter Duniho said:
Can you elaborate? I can see in Reflector that the framework (the Control
class, not the CLR) special-cases delegates of type EventHandler,

Ok, didn't check whether that was MSIL or internalcall. Doesn't much
matter, does it?
MethodInvoker, and WaitCallback. For those types, it casts directly
rather than calling Delegate.DynamicInvoke().

But do most people really care? Just what kind of performance difference
are we talking about here? Especially given all the other overhead
involved in calling Invoke() (after all, for the true cross-thread case it
involves a full context switch, which is surely at least as costly as any
of the extra work in DynamicInvoke()?).

Posting a message to another thread waits for the next context switch, it
doesn't insert an extra context switch.

Plus, DynamicInvoke is a very expensive operation.
Seems like a premature optimization to me. Is there some specific reason
to believe otherwise (especially given that the framework-defined Action
delegate type could be considered more expressive)?

The documentation (http://msdn.microsoft.com/en-us/library/zyzhdc6b.aspx)
says:
The delegate can be an instance of EventHandler, in which case the sender
parameter will contain this control, and the event parameter will contain
EventArgs..::.Empty. The delegate can also be an instance of MethodInvoker,
or any other delegate that takes a void parameter list. A call to an
EventHandler or MethodInvoker delegate will be faster than a call to another
type of delegate.

Also "premature optimization" assumes that there's a cost to the
optimization. Here, there's effectively no cost at all.
 
B

Barry Kelly

Ben said:
Also "premature optimization" assumes that there's a cost to the
optimization. Here, there's effectively no cost at all.

Knowing a piece of trivia is a cost :)

-- Barry
 

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