首页 > 解决方案 > 使用 WCF 服务自动向客户端发送值

问题描述

我有一个向 WPF 应用程序提供数据的服务,其中一个数据是一个名为 Market Price 的值,例如我希望它每分钟更新一次(我只是在做一个模拟,所以该服务将返回一个随机值)。所以我想知道最好和最简单的方法。

标签: wcf

解决方案


您可以尝试使用DispatcherTimer类。
在构造函数类上声明 this 并根据需要设置间隔:

var dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);//method to be executed
dispatcherTimer.Interval = new TimeSpan(0,0,1);//the interval is set to 1 second
dispatcherTimer.Start();

并声明一个要执行的方法:

private void dispatcherTimer_Tick(object sender, EventArgs e)
{
    //call WCF service and update the value
}

推荐阅读