简体   繁体   English

通过一个可能从多个线程触发的事件处理程序更新Winform ListView控件

[英]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.

相关问题 从自定义WinForm中的自定义事件处理程序访问控件 - Accessing Control from Custom Event Handler in Custom WinForm 听取从不同winform触发的自定义事件 - Listening for a custom event fired from different winform 覆盖WinForm ListView控件上的Drawitem事件 - Overriding the Drawitem event on a WinForm ListView control 双击将项目从一个列表框复制到另一个列表框。 Doubleclick事件未触发。 Winform C# - Copy item from one listbox to another on double click. Doubleclick event not fired. Winform C# 如何从一个控件调用事件处理程序到另一个控件? - How to call an event handler from one control to the another control? WPF:如何从事件处理程序中获取触发动画的控件的名称? - WPF: How to get the name of the control that fired an animation, from within the event handler? Winform中的事件处理程序 - Event Handler in Winform 是否有可能从一个控件“窃取”一个事件处理程序并将其交给另一个控件? - Is it possible to “steal” an event handler from one control and give it to another? 为什么Observable Collection仅在触发事件时更新ListView - Why Observable Collection is updating the ListView only when event is fired 在第二个控件位于第一个控件内的情况下,如何从一个控件调用事件处理程序到另一个控件? - How to call an event handler from one control to the another control where the second control is inside the first control?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM