Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF,在后台工作程序中调用服务器方法_Wpf_Multithreading_Timer - Fatal编程技术网

WPF,在后台工作程序中调用服务器方法

WPF,在后台工作程序中调用服务器方法,wpf,multithreading,timer,Wpf,Multithreading,Timer,我需要在wpf应用程序检查服务器上的消息。我有自己的方法在服务器上加载消息-LoadRp() 我想创建一种监听器,每3秒钟检查一次服务器上是否有新消息 我调用的方法用于加载调度程序计时器滴答事件上的消息,是否合适?任何其他解决方案。在wpf的另一个线程中是否可能调用计时器 代码如下: public MessangerWindow(PokecCommands pokecCmd) { InitializeComponent(); PokecCmd =

我需要在wpf应用程序检查服务器上的消息。我有自己的方法在服务器上加载消息-LoadRp(

我想创建一种监听器,每3秒钟检查一次服务器上是否有新消息

我调用的方法用于加载调度程序计时器滴答事件上的消息,是否合适?任何其他解决方案。在wpf的另一个线程中是否可能调用计时器

代码如下:

    public MessangerWindow(PokecCommands pokecCmd)
    {
        InitializeComponent();

        PokecCmd = pokecCmd;

        _friendsData = PokecCmd.LoadFriends();
        friendsListBox.DataContext = _friendsData;

        _dispatcherTimer = new DispatcherTimer();
        _dispatcherTimer.Tick+=new EventHandler(DispatcherTimer_Tick);
        _dispatcherTimer.Interval = new TimeSpan(0,0,3);
        _dispatcherTimer.Start();
    }

    private void DispatcherTimer_Tick(object sender, EventArgs eventArgs)
    {
        try
        {

            //try load new message from sever
            RP message = PokecCmd.LoadRp();

            //arived message
            if (message != null)
            {
                //exist window
                if (_chatWindows.ContainsKey(message.Nick))
                {
                    _chatWindows[message.Nick].Show();
                }
                {
                    //create new Window
                    var chatWindow = new ChatWindow(PokecCmd, message);
                    _chatWindows.Add(message.Nick, chatWindow);
                    chatWindow.Show();
                }
            }
        }
        catch (Exception ex)
        {
            //MessageBox.Show(ex.Message);
        }  
    }
什么是适合使用的:

  • 没有后台线程的调度程序
  • 具有后台线程的调度程序
  • 多线程

如果您可以在检查服务器所需的时间内锁定您的UI,那么使用Dispatcher的方式将可以正常工作

如果检查新消息可能需要几毫秒以上的时间,并且您希望UI在检查时能够响应,那么您应该使用多个线程。在这种情况下,一旦新数据到达,您将使用Dispatcher.Invoke来显示它

线程中检查消息的代码可能如下所示:

//try load new message from sever 
RP message = PokecCmd.LoadRp(); 

//arived message 
if( message != null )
    Dispatcher.Invoke(DispatcherPriority.Send, new Action(() =>
        { 
            //exist window 
            if (_chatWindows.ContainsKey(message.Nick)) 
            { 
                _chatWindows[message.Nick].Show(); 
            } 
            { 
                //create new Window 
                var chatWindow = new ChatWindow(PokecCmd, message); 
                _chatWindows.Add(message.Nick, chatWindow); 
                chatWindow.Show(); 
            } 
        }
 );