在C#中PostMessage的等效与MVVM主线程同步? [英] Equivalent of PostMessage in C# to synchronize with the main thread with MVVM?

查看:262
本文介绍了在C#中PostMessage的等效与MVVM主线程同步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须迟钝与搜索,因为这里是另一个看似常见的问题,我一直没能解决。

I must be retarded with searching, because here's another seemingly common problem that I haven't been able to solve.

下面是我的问题 - 我使用WPF与MVVM,我有一个模型执行的statemachine。如果发生错误,我需要传递信息到视图模型以显示错误。这部分似乎工作没关系。当用户点击期望的行为,该模型中的代码继续,并着眼于使用者,以确定下一步要做什么交互的对象

Here's my problem -- I am using WPF and MVVM, and I have a statemachine that executes in the model. If an error occurs, I need to pass information up to the ViewModel to display the error. This part seems to work okay. When the user clicks the desired behavior, the code in the model continues, and looks at the object the user interacts with to determine what to do next.

问题在于模型需要重新加载一个文件,该文件更新与所述文件的内容的图形用户界面。由于模型是在一个线程中执行,你能想象什么,我要问下一个 - 如何地狱,你的GUI正确同步?在MFC中,我会使用,也可以SendMessage函数或PostMessage的完成图形用户界面的更新。

The problem is that the model needs to reload a file, which updates the GUI with the contents of said file. Because the model is executing in a thread, you can imagine what I'm going to ask next -- how the hell do you synchronize with the GUI properly? In MFC, I would have used either SendMessage or PostMessage to accomplish the GUI update.

我已经的阅读建议使用InvokeRequired如有必要,会自动调用BeginInvoke为的WinForms 文章。我其实不知道的BeginInvoke会完成我想要的东西,所以我鼓励学习这一点。

I've read articles for WinForms that suggest using InvokeRequired to automatically call BeginInvoke if necessary. I actually didn't know that BeginInvoke would accomplish what I wanted, so I was encouraged to learn this.

如何其实我调用BeginInvoke从我的模型?请问这种方法甚至应用到WPF?我继续实行委托然后叫调用,但我得到了告诉我收集不能从这个线程修改了同样的错误。我也试过的BeginInvoke为它的地狱,但我认为也不会工作,因为它只是从不同的线程启动反正。

How do I actually call BeginInvoke from my model? Does this method even apply to WPF? I went ahead and implemented a delegate and then called Invoke, but I get the same error that tells me the collection can't be modified from this thread. I also tried BeginInvoke for the hell of it, but I assume that also wouldn't work because it would just launch from a different thread anyway.

困惑。如果我错过了一些东西真的很明显这就是被张贴了关于在互联网上,继续前进,给我口头绑扎,我是活该。

Confused. If I have missed something really obvious that's been posted about all over the internet, go ahead and give me a verbal lashing, I probably deserve it.

修改 - 我也许应该补充一点,我找了一个比定时器或基于BackgroundWorker的解决方案以外的东西,除非那是的只有的方式WPF / MVVM来解决这个问题。另外,我想知道如果任何MVVM工具包将对这种如果你想要的东西已经...

EDIT - I should probably also add that I am looking for something other than a timer or BackgroundWorker-based solution, unless that's the only way to solve this in WPF / MVVM. Also, I wonder if any of the MVVM toolkits would have facilities for this sort of thing already...

推荐答案

设施安排从后台线程在WPF UI线程一些工作,使用 DispatcherObject的 。下面是关于如何构建更加适应应用与调度一个很好的文章。

If you want to schedule some work from a background thread to the UI thread in WPF, use the DispatcherObject. Here's a nice article on how to Build More Responsive Apps with the Dispatcher.

更新:注意,如果你使用一个事件从模型到视图模型发送通知,你仍然需要地方切换到UI线程。无论是开关应该是在模型或视图模型是一个不错的设计讨论,但它是正交你的问题。

Update: Note that if you use an event to send notifications from the Model to the ViewModel, you still need to switch to the UI thread somewhere. Whether that switch should be in the Model or the ViewModel is a good design discussion, but it's orthogonal to your question.

本次活动将相应的调度线程上提出的。因为你需要去UI线程,你需要使用在UI线程上创建的调度。得到一个最简单的方法是使用 DispatcherObject.Dispatcher 财产上的UI元素之一。另一种方法是创建一个模型中的视图模型或。如果你是一个纯粹的设计,我建议你在你的模型创建调度和派遣回调到UI线程你提高到视图模型监听的事件之前。这样,所有的线程切换和管理包含在模型和视图模型可以作为一个单线程仅在UI线程上。

The event will be raised on the corresponding Dispatcher thread. Since you need to get to the UI thread, you need to use a Dispatcher that is created on the UI thread. The easiest way to get one is to use the DispatcherObject.Dispatcher property on one of the UI elements. The alternative is to create one in your Model or ViewModel. If you are a design purist, I would suggest you create the Dispatcher in your Model and dispatch the call back to the UI thread before you raise the event to which the ViewModel is listening. This way all the thread switching and management is contained in your Model and the ViewModel works as a single-threaded on the UI thread only.

这篇关于在C#中PostMessage的等效与MVVM主线程同步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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