我想調用一個在UI線程上操作控件的方法。我的代碼有效,我想優化。我指的是這個resource on MSDN。
據那裏,我們應該做的Winforms multithreading:每次在UI線程上調用方法時都需要創建一個新的委託?
public delegate void myDelegate(int anInteger, string aString);
//...
Label1.Invoke(new myDelegate(myMethod), new Object[] {1, "This is the string"});
這會引入一個孤立的委託對象(內存泄漏),在每次調用?
當我將與委託的靜態實例做類似下面,然後用這個實例在每次調用調用:
private static _delegateInstance = new myDelegate(myMethod);
//...
Label1.Invoke(_delegateInstance , new Object[] {1, "This is the string"});
請問這是線程安全的?因爲委託實例只創建一次,所以我認爲它的性能略好一些?
謝謝,這個技巧非常好! – Marcel 2010-11-26 10:37:16