teixeira Posted October 12, 2005 Posted October 12, 2005 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 Quote
Leaders snarfblam Posted October 12, 2005 Leaders Posted October 12, 2005 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. Quote [sIGPIC]e[/sIGPIC]
IngisKahn Posted October 12, 2005 Posted October 12, 2005 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. Quote "Who is John Galt?"
teixeira Posted October 12, 2005 Author Posted October 12, 2005 Thanks for all the explanation and translation. I will be v ery helpfull. Thanks both Teixeira Quote
Leaders snarfblam Posted October 12, 2005 Leaders Posted October 12, 2005 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? Quote [sIGPIC]e[/sIGPIC]
IngisKahn Posted October 13, 2005 Posted October 13, 2005 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. Quote "Who is John Galt?"
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.