使用C#调度 [英] Using the C# Dispatcher

查看:189
本文介绍了使用C#调度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个聊天客户端,我不100%确定如何使用调度。所以,问题是我有一个方法,例如:

I'm building a chat client and am not 100% sure on how to use the dispatcher. So the question is I have a method as such:

public void LostConnection()
{
    myGUI.chatBox.AppendText("Lost connection to room: "+ myGUI.UsernameText.ToString() + "\r\n");
}

我是否需要内用来包围声明(myGUI.chatBox ...) Dispatcher.Invoke ?我AP preciate任何帮助。

Do i need to surrond the statement within (myGUI.chatBox... ) with a Dispatcher.Invoke? I appreciate any help.

推荐答案

您的应用程序有一个主UI线程(通常 ManagedThreadId == 1 )。通常,在一个聊天应用程序的事件会在其他线程(或专用插座听从听code线程或线程池中的线程)。如果你想从获取拉,你必须使用调度其他线程的事件更新UI。这里的一个有用的测试是 Dispatcher.CheckAccess()方法,返回true,如果code是在UI线程和虚假如果其他线程。一个典型的调用看起来是这样的:

Your app has a main UI thread (usually ManagedThreadId==1). Typically in a chat app your events will come in on other threads (either dedicated socket listen threads or thread pool threads from listening code). If you want to update the UI from an event that gets pull on some other thread you must use the dispatcher. A useful test here is the Dispatcher.CheckAccess() method that returns true if code is on UI thread and false if on some other thread. A typical call looks something like:

using System.Windows.Threading; // For Dispatcher.

if (Application.Current.Dispatcher.CheckAccess()) {
    network_links.Add(new NetworkLinkVM(link, start_node, end_node));
}
else {
    Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(()=>{
        network_links.Add(new NetworkLinkVM(link, start_node, end_node));
    }));
}

如果您在主窗口是你可以使用:

If you're in the main window you can use:

Dispatcher.BeginInvoke(...

如果您在someother方面如视图模型是再使用:

If you're in someother context eg a view model then use:

Application.Current.Dispatcher.BeginInvoke(  

调用VS的BeginInvoke 结果
使用调用如果你想要当前线程等待,直到UI线程处理,调度code或的BeginInvoke 如果你想当前线程继续,而不必等待操作完成的UI线程。

Invoke vs BeginInvoke
Use Invoke if you want the current thread to wait until the UI thread has processed the dispatch code or BeginInvoke if you want current thread to continue without waiting for operation to complete on UI thread.

的MessageBox,调度员和调用/ BeginInvoke的:结果
Dispatcher.Invoke 将阻止您的线程,直到消息框被驳回。结果
Dispatcher.BeginInvoke 将让你的线程code将继续执行,而UI线程将在MessageBox的呼叫阻塞,直到其解雇。

MessageBox, Dispatchers and Invoke/BeginInvoke:
Dispatcher.Invoke will block your thread until the MessageBox is dismissed.
Dispatcher.BeginInvoke will allow your thread code to continue to execute while the UI thread will block on the MessageBox call until its dismissed.

CurrentDispatcher VS Current.Dispatcher!结果
要洁具 Dispatcher.CurrentDispatcher ,因为我的这种理解是将返回调度当前线程不是UI线程。通常你有兴趣在UI线程调度 - Application.Current.Dispatcher 总是返回此

CurrentDispatcher vs Current.Dispatcher!
Be ware of Dispatcher.CurrentDispatcher as my understanding of this is that is will return a Dispatcher for the current thread not the UI thread. Generally are you interested in the dispatcher on the UI thread - Application.Current.Dispatcher always returns this.

附加说明:结果
如果您发现您有经常检查调度的checkAccess然后有用的帮手,方法是:

Additional note:
If you are finding you are having to check dispatcher CheckAccess often then a useful helper method is:

public void DispatchIfNecessary(Action action) {
    if (!Dispatcher.CheckAccess())
        Dispatcher.Invoke(action);
    else
        action.Invoke();
}

这才叫为:

DispatchIfNecessary(() => {
    network_links.Add(new NetworkLinkVM(link, start_node, end_node));
});

这篇关于使用C#调度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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