从另一个线程更新绑定的WPF控件 [英] Update bound WPF controls from another thread

查看:130
本文介绍了从另一个线程更新绑定的WPF控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前遇到的问题是从工作线程更新WPF控件。

在Google上搜索几个小时后,我希望在这里得到答案。



情况:

- 我有一个GUI

- GUI上有一个视图模型

- 视频模型包含绑定的所有属性。它包含int,float,bool和string属性值以及int []数组。

- 所有属性如下所示:

  private   volatile   string  _InputTypeString =  ; 
public string InputTypeString
{
get { return _InputTypeString; }
set {_InputTypeString = value ; OnPropertyChanged( InputTypeString); }
}



- 所有控件都通过XML绑定绑定到viewmodel

- 当我这样做时不使用GUI更新,我的代码运行好几个小时,一旦我更新GUI我会在几分钟内遇到问题。



什么我试过了:



我已经尝试了什么:



- 制作所有变量物业对象不稳定



- 代表通过

行动b =  delegate  {windowmodel.Frame_Current = FrameX}; 
Application.Current.Dispatcher.Invoke(b,DispatcherPriority.Normal);



- 锁:

  lock (windowmodel.AudioLevelArray)







任何想法如何让GUI相关属性线程安全?

解决方案

在几分钟内遇到问题





有什么问题?从问题的其余部分来看,你可能会怀疑多线程是问题所在。但是你没有显示任何与此相关的代码,你提到了这些问题的一些默认解决方案模式,但我们如何判断你是否正确应用它们?



我可以说(多年来与WPF和MVVM合作)多线程如果你正确地从其他线程发送更改就没问题。



所以如果你的(异步)方法不报告进度 - 只需在正确的(GUI)线程上报告结果(通过调度) - 不要使用任何共享变量来简化生活。



但是我怀疑你可能还有其他一些问题...因为如果你做错了调度它通常不会花费几分钟来产生错误(或者是你的异步方法运行了那么久?)



所以我现在停止quesswork - 向我们展示真正的错误或异常 - 然后我们可以尝试找到原因..


I currently run into the problem to update WPF controls from a worker thread.
After searching on Google for hours I hope to get an answer here.

Situation:
- I do have a GUI
- The GUI has a viewmodel attached to it
- The videmodel contains all properties for the binding. This contains int, float, bool and string property values as well as an int[] array.
- All properties look like this:

private volatile string _InputTypeString = "";
public string InputTypeString
{
    get { return _InputTypeString; }
    set { _InputTypeString = value; OnPropertyChanged("InputTypeString"); }
}


- All of the controls are bound to the viewmodel via XML bindings
- When I do not use the GUI updates, my Code runs fine for hours and as soon as I update the GUI I run into problems within a couple of minutes.

What I have tried:

What I have already tried:

- make all variables of the property objects volatile

- delegates via

Action b = delegate { windowmodel.Frame_Current = FrameX };
Application.Current.Dispatcher.Invoke(b, DispatcherPriority.Normal);


- locks:

lock (windowmodel.AudioLevelArray)




Any idea how to get the GUI related properties thread-safe?

解决方案

run into problems within a couple of minutes



What Problems? From the rest of the question it sounds like you suspect multithreading to be the problem. But you didn't show any code related to that, you mentioned some default "solution patterns" for such Problems, but how can we tell if you applied them correctly?

What I can say (working with WPF and MVVM for years) that multithreading is no problem if you dispatch changes from other threads correctly.

So if your (async) method doesn't report Progress - just report back the result on the correct (GUI)-thread (by dispatching) - don't use any shared variables to make your life easy.

But I suspect you may have some other problem... because if you did something wrong with dispatching it normally won't take minutes to generate an error (or does your async method runs for that long?)

So I stop quesswork now - Show us the real error or exception - then we can try to find the reason..


这篇关于从另一个线程更新绑定的WPF控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆