我有一个带有两个列表视图的 WPF 程序,它们显示来自两个不同可观察集合的信息。这些可观察的集合在每秒触发的计时器上更新。
由于某种原因,这些列表视图在数据收集更新时不会更新......它们偶尔会更新,但不能保证它会发生。我可以运行整个程序 10 分钟,但没有显示任何内容...输出窗口中也没有数据绑定错误。事实上,如果我在启动期间填充集合以测试绑定,则所有显示都没有问题。只有当我在程序执行期间动态添加项目时才会出现问题。
当尝试调试时,不可能遵循该程序,因为由于某种原因,我已经运行了一整堆工作线程,而我从未定义或要求过这些线程:
https://i.sstatic.net/Z0XB3.png
我认为这些额外的线程可能是问题的一部分。 C#中有没有一种简单的方法可以像Java中的synchronized关键字一样将函数定义为线程安全的?
编辑:
JFifoErrorCollection.Add(errorData);
JFifoList.Items.Refresh();
break;
编辑#2:我已经解决了这个问题..我使用的是 System.Threading.Timer,它没有在 UI 线程上运行。更改为 System.Windows.Forms.Timer 现在可以完美运行。
I have a WPF program with two listviews which display information from two different observable collections. These observable collections are updated on a timer which fires every second.
For some reason, these listviews do not update when the data collection is updated... they will update occasionally but there is no guarantee that it will ever happen. I can run the whole program for 10 minutes and have nothing displayed... There are no databinding errors in the output window, either. In fact, if I populate the collection during startup, to test the binding, it is all displayed without problems. It's only when I am adding items dynamically during the execution of the program that there is a problem.
When trying to debug it is impossible to follow the program because for some reason I've got a whole load of worker threads running which I never defined or asked for:
https://i.sstatic.net/Z0XB3.png
I think these extra threads might be part of the problem. Is there an easy way in C# to define a function as threadsafe like the synchronized keyword in Java?
EDIT:
JFifoErrorCollection.Add(errorData);
JFifoList.Items.Refresh();
break;
EDIT #2: I have solved this issue.. I was using a System.Threading.Timer which didn't run on the UI thread. Changed to System.Windows.Forms.Timer and now it works perfectly.
发布评论
评论(1)
使用 DispatcherTimer 中内置的 WPF。
Use the WPF built in DispatcherTimer.