[英]Updating Winform ListView control by one event handler that may be fired from several threads
I have a "Win Form" application which listening to an event that may be raised from some different threads. 我有一个“ Win Form”应用程序,该应用程序侦听可能从某些不同线程引发的事件。
on that event i want to update a "List View" control. 在该事件上,我想更新“列表视图”控件。 of course that i may need to use Invoke but my question is: Should i use lock on the function i will build to update the "List View" control or not?
当然,我可能需要使用Invoke,但我的问题是:我应该在要构建的函数上使用锁来更新“列表视图”控件吗?
Be a ware that i am updating the List View only by one function in the form and by only one event but more than one thread may raise the same event. 请注意,我仅通过表单中的一个函数并且仅通过一个事件来更新列表视图,但是一个以上的线程可能会引发同一事件。
is it true that all the events that may be fired from all the threads will be handled by the main thread only so they will be executed one by one? 是否所有线程可能触发的所有事件仅由主线程处理,以便它们将被一个一个地执行?
Thanks. 谢谢。
If you think the event could be fired at the same time from multiple threads, or at least get fired again before finishing the update then yes you should lock. 如果您认为该事件可能同时在多个线程中触发,或者至少在完成更新之前再次触发,那么您应该锁定。
Something simple like: 很简单的东西:
lock(myListView) {
updateMyListView();
}
should be enough. 应该足够了。 Locking on the list view object should still let you edit it.
锁定列表视图对象仍应允许您对其进行编辑。
A word of warning, this won't keep other things from editing the list view, only code that has it's own lock(myListView)
block will pay attention to the lock. 提醒一下,这不会阻止其他事情编辑列表视图, 只有具有自己的
lock(myListView)
块的代码才会注意该锁。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.