对于每个溶液波纹管我运行131072(128×1024)的迭代(在一个独立的线程)。 的VS2010演奏助手把这个结果:
- 只读MethodInvoker:5664.53(+ 0%)
- 新MethodInvoker:5828.31(+ 2.89%),在MethodInvoker
- 功能转换:5857.07(3.40 %)
- 只读操作:6467.33(+ 14.17%)
- 新动作:6829.07(+ 20.56%)
呼叫到一个新行动在每次迭代
private void SetVisibleByNewAction()
{
if (InvokeRequired)
{
Invoke(new Action(SetVisibleByNewAction));
}
else
{
Visible = true;
}
}
呼叫到一个只读,建立构造,行动在每次迭代
// private readonly Action _actionSetVisibleByAction
// _actionSetVisibleByAction= SetVisibleByAction;
private void SetVisibleByAction()
{
if (InvokeRequired)
{
Invoke(_actionSetVisibleByAction);
}
else
{
Visible = true;
}
}
呼叫到一个新的MethodInvoker在每次迭代。
private void SetVisibleByNewMethodInvoker()
{
if (InvokeRequired)
{
Invoke(new MethodInvoker(SetVisibleByNewMethodInvoker));
}
else
{
Visible = true;
}
}
呼叫到一个只读的,在每次迭代
// private readonly MethodInvoker _methodInvokerSetVisibleByMethodInvoker
// _methodInvokerSetVisibleByMethodInvoker = SetVisibleByMethodInvoker;
private void SetVisibleByMethodInvoker()
{
if (InvokeRequired)
{
Invoke(_methodInvokerSetVisibleByMethodInvoker);
}
else
{
Visible = true;
}
}
调用该函数铸于MethodInvoker建立在构造函数中,MethodInvoker在每次迭代
private void SetVisibleByDelegate()
{
if (InvokeRequired)
{
Invoke((MethodInvoker) SetVisibleByDelegate);
}
else
{
Visible = true;
}
}
呼叫“新行动”解决方案的示例:
private void ButtonNewActionOnClick(object sender, EventArgs e)
{
new Thread(TestNewAction).Start();
}
private void TestNewAction()
{
var watch = Stopwatch.StartNew();
for (var i = 0; i < COUNT; i++)
{
SetVisibleByNewAction();
}
watch.Stop();
Append("New Action: " + watch.ElapsedMilliseconds + "ms");
}
看看这个问题,以及http://mark-dot-net.blogspot.com.uy/2014/07/six-ways-to-initiate-tasks-on-another.html – 2016-09-08 14:54:54