星空网 > 软件开发 > ASP.net

WPF中使用定时器 DispatcherTimer 做TCP连接中的心跳 HeartBeat

开发过程中经常遇到定时触发的需求,如:TCP/IP连接中,使用心跳包保持连接或检测连接是否已经中断。

WPF中有多种定时器:

1.using System.Windows.Threading;

代码如下:

using System.Windows.Threading;public partial class MainWindow : Window{  DispatcherTimer timerHeartBeat = new DispatcherTimer();  public MainWindow()  {    timerHeartBeat.Tick += SendHeartBeatToServer; 
timerHeartBeat.Interval = TimeSpan.FromSeconds(60 * 5);
timerHeartBeat.Start(); } private void SendHeartBeatToServer(object sender,EventArgs e) { string strToSend = "{CMD:'99999'}"; SendMsg(strToSend); }}

 




原标题:WPF中使用定时器 DispatcherTimer 做TCP连接中的心跳 HeartBeat

关键词:wpf

wpf
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

欧洲小包服务:https://www.goluckyvip.com/tag/102537.html
怎么寄国际包裹:https://www.goluckyvip.com/tag/102538.html
个人寄国际包裹:https://www.goluckyvip.com/tag/102539.html
ems怎么寄国际包裹:https://www.goluckyvip.com/tag/102540.html
邮政小包国际件:https://www.goluckyvip.com/tag/102541.html
平邮欧洲小包:https://www.goluckyvip.com/tag/102542.html
恐怖游轮2002 恐怖游轮2022:https://www.vstour.cn/a/365178.html
时尚电商平台Meesho拟融资3亿美元!:https://www.kjdsnews.com/a/1836524.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流