简体   繁体   English

锁定调度员

[英]Locking dispatcher

Is it necessary to lock code snippet where multiple threads access same wpf component via dispatcher?是否有必要锁定多个线程通过调度程序访问同一个 wpf 组件的代码片段?

Example:例子:

void ladder_OnIndexCompleted(object sender, EventArgs args)
{
    lock (locker)
    {
        pbLadder.Dispatcher.Invoke(new Action(() => { pbLadder.Value++; }));
    }
}

pbLadder is a progress bar and this event can be raised from multiple threads in the same time. pbLadder是一个进度条,可以同时从多个线程引发此事件。

You should not acquire a lock if you're then going to marshal to another thread in a synchronous fashion - otherwise if you try to acquire the same lock in the other thread (the dispatcher thread in this case) you'll end up with a deadlock. 应该获得一个锁,如果你再要元帅以同步的方式另一个线程-否则,如果你试图获取其他线程相同的锁(在这种情况下,调度程序线程),你会用一个结束僵局。

If pbLadder.Value is only used from the UI thread, then you don't need to worry about locking for thread safety - the fact that all the actions occur on the same thread isolates you from a lot of the normal multi-threading problems. 如果pbLadder.Value UI线程使用pbLadder.Value ,那么您不必担心锁定线程安全性 - 所有操作都发生在同一个线程上的事实将您与许多正常的多线程问题隔离开来。 The fact that the original action which caused the code using pbLadder.Value to execute occurred on a different thread is irrelevant. 导致使用pbLadder.Value执行代码的原始操作发生在另一个线程上的事实是无关紧要的。

All actions executed on the Dispatcher are queued up and executed in sequence on the UI thread. Dispatcher上执行的所有操作都排队并在UI线程上按顺序执行。 This means that data races like that increment cannot occur. 这意味着不会发生类似增量的数据竞争。 The Invoke method itself is thread-safe, so also adding the action to the queue does not require any locking. Invoke方法本身是线程安全的,因此将操作添加到队列也不需要任何锁定。

From MSDN : 来自MSDN

Executes the specified delegate with the specified arguments synchronously on the thread the Dispatcher is associated with. 在与Dispatcher关联的线程上同步执行具有指定参数的指定委托。

and: 和:

The operation is added to the event queue of the Dispatcher at the specified DispatcherPriority. 该操作将添加到指定DispatcherPriority的Dispatcher的事件队列中。

Even though this one is pretty old it was at the top of my search results and I'm pretty new (4 months since I graduated) so after reading other peoples comments, I went and spoke with my senior coder.尽管这个很旧,但它在我的搜索结果中排在首位,而且我很新(毕业 4 个月后)所以在阅读了其他人的评论后,我去和我的高级编码员交谈。 What the others are saying above is accurate but I felt the answers didn't provide a solution, just information.上面其他人所说的是准确的,但我觉得答案没有提供解决方案,只是信息。 Here's the feedback from my senior coder:这是我的高级编码员的反馈:

"It's true that the Dispatcher is running on its own thread, but if another thread is accessing an object that the dispatcher wants to access then all UI processing stops while the dispatcher waits for the access. To solve this ideally, you want to make a copy of the object that the dispatcher needs to access and pass that to the dispatcher, then the dispatcher is free to edit the object and won't have to wait on the other thread to release their lock." “Dispatcher 确实在自己的线程上运行,但如果另一个线程正在访问 object,而调度程序想要访问,那么在调度程序等待访问时,所有 UI 处理都会停止。为了理想地解决这个问题,你想做一个调度员需要访问的 object 的副本并将其传递给调度员,然后调度员可以自由编辑 object,而不必等待其他线程释放他们的锁。”

Cheers!干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM