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