.NET: Control Invoke() in Reflector
Asked Answered
G

3

1

So, I was getting back into some .NET programming, and through a new feature in VS.NET 2010, it detected a case where I was trying to modify a control from a thread that didn't create that control, and pointed me to an article on MSDN about how you do this correctly...

' HOW TO WRITE TO A FORM CONTROL FROM A THREAD THAT DIDN'T CREATE THE CONTROL
' ===========================================================================
' Say you need to write to a UI text box that logs stuff...
Delegate Sub WriteLogDelegate(ByVal [text] As String)
Private Sub WriteLog(ByVal [text] As String)
    If Me.rtfLog.InvokeRequired Then
        ' We are not in the same thread!
        ' Create new WriteLogDelegate and invoke it on the same thread
        Dim d As New WriteLogDelegate(AddressOf WriteLog)
        Me.rtfLog.Invoke(d, New Object() {[text]})
    Else
        ' We are totally in the same thread...
        ' Call AppendText like normal!
        Me.rtfLog.AppendText([text])
    End If
End Sub

And i was so excited because i have been puzzled by how to do this for like 5 years because previous versions of vs.net didn't flag this since i was an undergrad on a project and...

Umm... Sorry bout that. Composure regained. Anyway, now that I know this bit of .NET-fu, I'd like to learn more about what's going on and how it works.

Where can I find the code for Invoke() in .NET Reflector?

Gautea answered 9/5, 2010 at 19:3 Comment(4)
It's on System.Windows.Forms.Control. Also, I don't like caps.Byproduct
There's so much unmanaged code in WinForms that you might not get very far with Reflector. Anyway, what exactly you would like to know about it?Springs
ditto what they said. The code path you are interested in spans multiple namespaces and gets deep and gnarly with a quickness. What you need to know is how to use Invoke and more probably BeginInvokeSparrow
The caps were a joke - they were trying to express my excitement. Attempt to be humorous.Gautea
K
1

Well, let name all thing that happening in your example.

  1. The code that you showing there is cross-threading or multi-threading programing.(that exist from beginning of the dotnet).
  2. Your rtfLog is using standard capabilities of InvokeRequired - this is mean, that rtfLog is inheriting from Control class.
    2.a. Control class is part of Winforms structure.
  3. To reach the code that "' We are not in the same thread!" you need to create two or more Threads.

Overall, the reflector can show you the implementation, but the idea you can find in article.
If you shure that you want to see the implementation, just compile some short code like in "code poet" answer, and look at him in Reflector.(I check this part, the reflector will show you something that pretty close your original code.)

Keister answered 9/5, 2010 at 23:57 Comment(0)
S
1

This should be a comment but cannot code format in comments so....

If you were using c# this story could get even simpler..

private void WriteLog(string text)
{
  if(InvokeRequired)
  {
    BeginInvoke(new MethodInvoker(()=>{ WriteLog(text); }));
  }
  else
  {
    rtfLog.AppendText(text);
  }
}
Sparrow answered 9/5, 2010 at 19:26 Comment(0)
K
1

Well, let name all thing that happening in your example.

  1. The code that you showing there is cross-threading or multi-threading programing.(that exist from beginning of the dotnet).
  2. Your rtfLog is using standard capabilities of InvokeRequired - this is mean, that rtfLog is inheriting from Control class.
    2.a. Control class is part of Winforms structure.
  3. To reach the code that "' We are not in the same thread!" you need to create two or more Threads.

Overall, the reflector can show you the implementation, but the idea you can find in article.
If you shure that you want to see the implementation, just compile some short code like in "code poet" answer, and look at him in Reflector.(I check this part, the reflector will show you something that pretty close your original code.)

Keister answered 9/5, 2010 at 23:57 Comment(0)
F
0

Here's Control.Invoke on MSDN: http://msdn.microsoft.com/en-us/library/zyzhdc6b.aspx

Farther answered 9/5, 2010 at 19:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.