Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi all,

 

I'm developing an application terminal to read a serial port, and i'm using VB.NET 2005 Team System Beta2, and when i received the data (text) i wanna put it into a textbox but an error appears telling that is not possible to access to the textbox that is in another thread :-(..... and i was searching trought the web about it and i found a code snipet in C# and i would like to have this translated to VB.NET

here's the code

 

private void Log( string msg)

{

rtfTerminal.Invoke(new EventHandler(delegate

{

rtfTerminal.AppendText(msg);

}));

}

 

 

Appearly this code solves my problem, if anyone could translate it i would be very greatfull

 

Thanks in advance

 

Teixeira

  • Leaders
Posted

The code snippet you provided uses a feature new to C# 2.0 called anonymous methods. This is basically a nameless method that can be declared within another method. I hilighted the anonymous method below.

private void Log( string msg)
{
rtfTerminal.Invoke(new EventHandler([b][color=red]delegate
{
rtfTerminal.AppendText(msg);
}[/color][/b]));
}

I am not to keen on the syntax for anonymous methods, but it seems to me that the anonymous method should have been declared with the appropriate signature for an EventHandler, and I don't quite understand how msg is passed to the anonymous method.

 

If we can get past my confusion, the above code listing would be the same (in terms of behavior) as:

private void Log( string msg)
{
rtfTerminal.Invoke(new EventHandler(anotherFunction));
}
//anotherFunction calls rtfTerminal.AppendText

 

Once we are at this point, the translation is pretty easy.

Private Sub Log(msg As String)
   rtfTerminal.Invoke(AddressOf anotherFunction)
End Sub

'anotherFunction calls rtfTerminal.AppendText

 

In other words, to modify UI elements from another thread, you simply need to call the control's Invoke() method, passing a delegate to the function that will modify the control, and if there are any parameters, there is an overload that allows you to pass them in an array.

 

Maybe someone more C# savvy could clear up my confusion about the anonymous methods, but I hope I helped.

[sIGPIC]e[/sIGPIC]
Posted

An anonymous method does not need to specify delegate arguments if it does not need them (all or nothing).

 

When you assign an anonymous method any variables defined outside that method (in this case "msg") have their values "hard coded" into that method.

"Who is John Galt?"
  • Leaders
Posted
When you assign an anonymous method any variables defined outside that method (in this case "msg") have their values "hard coded" into that method.

Does this mean that the anonymous method is re-JITted each time it is used?

[sIGPIC]e[/sIGPIC]
Posted

Actually, no.

If you can manage to decipher the mess of IL that is generated for anonymous methods you'd see that it creates a wrapper class for each method that contains any external values in that method as fields.

"Who is John Galt?"

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...