我知道,当从任何非UI线程操作UI控件时,您必须封送对UI线程的调用以避免问题.一般的共识是您应该使用测试InvokeRequired,如果为true,则使用.Invoke来执行封送处理.
这会导致很多代码看起来像这样:
private void UpdateSummary(string text)
{
if (this.InvokeRequired)
{
this.Invoke(new Action(() => UpdateSummary(text)));
}
else
{
summary.Text = text;
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:我可以省略InvokeRequired测试并只调用Invoke,如下所示:
private void UpdateSummary(string text)
{
this.Invoke(new Action(() => summary.Text = text));
}
Run Code Online (Sandbox Code Playgroud)
这样做有问题吗?如果是这样,是否有更好的方法来保持InvokeRequired测试,而不必在整个地方复制和粘贴此模式?
在几个 showstoppers延迟迁移到.NET 4.6运行时之后,我终于习惯于转移到C#6/VB14编译器,直到遇到VB.NET中的迭代器函数抛弃局部变量的关键问题.
在Visual Studio 2015/msbuild中以发布模式(优化)编译时,以下代码示例将在注释行上抛出空引用异常.
Module Module1
Sub Main()
For Each o As Integer In GetAllStuff()
Console.WriteLine(o.ToString())
Next
Console.ReadKey()
End Sub
Private Iterator Function GetAllStuff() As IEnumerable(Of Integer)
Dim map As Dictionary(Of String, String) = New Dictionary(Of String, String)
Dim tasks As New List(Of Integer)
tasks.Add(1)
For Each task As Integer In tasks
Yield task
Next
'The value of map becomes null here under the new VB14 compiler in Release on .NET 4.6'
For Each s As …
Run Code Online (Sandbox Code Playgroud)