我想调用一个在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