對於每個溶液波紋管我運行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