.NET:反射器中的控制 Invoke()

发布于 2024-09-01 09:40:25 字数 1073 浏览 3 评论 0 原文

因此,我重新开始一些 .NET 编程,通过 VS.NET 2010 中的一项新功能,它检测到我试图从未创建该控件的线程修改该控件的情况,并向我指出MSDN 上有一篇关于如何正确执行此操作的文章...

' 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

我非常兴奋,因为我对如何执行此操作感到困惑了大约 5 年,因为以前版本的 vs.net 没有标记这一点,因为我还是一名本科生一个项目和……

嗯……抱歉。恢复了冷静。不管怎样,现在我已经了解了 .NET-fu 的一些知识,我想了解更多关于正在发生的事情以及它是如何工作的。

在哪里可以找到 .NET Reflector 中 Invoke() 的代码?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

淡水深流 2024-09-08 09:40:25

好吧,让我们说出您的示例中发生的所有事情。

  1. 您在那里显示的代码是跨线程或多线程编程。(从 dotnet 开始就存在)。
  2. 您的rtfLog正在使用InvokeRequired的标准功能 - 这意味着rtfLog继承自Control类
    2.a.控件类是Winforms结构的一部分。
  3. 达到代码“'我们不在同一个线程!”您需要创建两个或多个线程

总的来说,反射器可以向您展示实现,但您可以在文章中找到想法.
如果您确定想要查看实现,只需编译一些简短的代码,如“代码诗人”答案中的代码,然后在 Reflector。(我检查了这部分,reflector 会向您显示与原始代码非常接近的内容。)

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.)

烂人 2024-09-08 09:40:25

这应该是一个注释,但不能在注释中编码格式,所以......

如果你使用 c# 这个故事可能会变得更简单......

private void WriteLog(string text)
{
  if(InvokeRequired)
  {
    BeginInvoke(new MethodInvoker(()=>{ WriteLog(text); }));
  }
  else
  {
    rtfLog.AppendText(text);
  }
}

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);
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文