【发布时间】:2013-09-06 17:00:55
【问题描述】:
我在一些我正在开发的程序中进行了很多线程化工作,我一直很好奇某些东西到底在做什么。
以下面的代码为例,它从一个线程运行以更新 UI:
Public Sub UpdateGrid()
If Me.InvokeRequired Then
Me.Invoke(New MethodInvoker(AddressOf UpdateGrid))
Else
DataGridView1.DataSource = dtResults
DataGridView1.Refresh()
btnRun.Text = "Run Query"
btnRun.ForeColor = Color.Black
End If
End Sub
Me.InvokeRequired 究竟检查什么,Me.Invoke 究竟在做什么?我知道它以某种方式让我可以访问 UI 上的项目,但它是如何实现的呢?
顺便说一句,假设 UpdateGrid() 是一个返回值并具有必需参数的函数。调用 Me.Invoke 方法后如何传递参数以及如何获取返回值?我在没有参数的情况下尝试了此操作,但返回“nothing”,并且在调用时我无法弄清楚如何附加参数。
【问题讨论】: