Dog*_*ars 9 c# multithreading winforms
如果我在UI上调用一个方法线程是否由Windows消息队列进行搜索,随后不需要重入?
private void CalledFromWorkerThread()
{
//changed from 'InvokeRequired' Anti-Pattern
this.Invoke((Action) (() => _counter++; /* Is this ok? */ ));
}
Run Code Online (Sandbox Code Playgroud)
澄清:只有UI线程才会访问_counter.
你有什么是好的,假设_counter只有UI线程访问.
如果两个线程调用你的CalledFromWorkerThread,那么_counter将适当增加并且使用你拥有的线程安全.