C#定时执行一个操作
一个客户端向服务器端socket发送报文,但是服务器端限制了发送频率,假如10秒内只能发送1次,这时客户端也要相应的做限制,初步的想法是在配置文件中保存上次最后发送的时间,当前发送时和这个上次最后时间做比较,根据情况马上发送还是休眠相应的时间。
举个例子,服务器发送频率限制是10秒,上次最后发送时间是10:00:00,有两种情况:
(1)当前时间是10:00:03,则过7秒后发送;
(2)当前时间是10:02:00,则马上发送。
App.config
<!--发送频率限制(秒)-->
<add key="MsgTimeLimit" value="10"/>
<!--上次最后发送时间-->
<add key="LastMsgTime" value="2013-11-1"/>
Test.cs
CancellationTokenSource ct;
private void btnOK_Click(object sender, EventArgs e)
{
btnOK.Enabled = false; Task t = new Task(() => Do(ct));
ct = new CancellationTokenSource();
t.Start();
t.ContinueWith((x) =>
{
this.SafeCall(() =>
{
richTextBox1.AppendText("任务结束\r\n");
btnOK.Enabled = true;
});
});
}
private void btnCancel_Click(object sender, EventArgs e)
{
ct.Cancel();
}
private void Do(CancellationTokenSource ct)
{
for (int i = 0; i < 3; i++)
{
if (!ct.IsCancellationRequested)
{
int restSeconds = GetMsgRestSeconds();
if (restSeconds > 0)
{
SetTextBoxText("请等待,暂停 " + restSeconds + " 秒\r\n");
Thread.Sleep(restSeconds * 1000);
}
SetTextBoxText("正在发送第" + (i + 1).ToString() + "个客户...\r\n");
AppSettings.SetValue("LastMsgTime", DateTime.Now.ToString());
}
else
{
SetTextBoxText("任务" + (i + 1).ToString() + "取消\r\n");
}
}
}
/// <summary>
/// 获取发送剩余的时间
/// </summary>
/// <returns></returns>
private int GetMsgRestSeconds()
{
int msgTimeLimit = 0;
//获取要限制的间隔时间(秒)
int.TryParse(AppSettings.GetValue("MsgTimeLimit"), out msgTimeLimit);
if (msgTimeLimit == 0)
return 0;
//最近一次时间
string lastMsgTime = AppSettings.GetValue("LastMsgTime");
DateTime dtLastMsgTime = DateTime.MinValue;
DateTime.TryParse(lastMsgTime, out dtLastMsgTime);
DateTime dtNow = DateTime.Now;
if (dtLastMsgTime == DateTime.MinValue || dtLastMsgTime >= dtNow)
return 0;
TimeSpan ts = dtNow - dtLastMsgTime;
int restSeconds = 0;
if (msgTimeLimit > ts.TotalSeconds)
{
restSeconds = msgTimeLimit - (int)ts.TotalSeconds;
restSeconds = restSeconds < 0 ? 0 : restSeconds;
}
return restSeconds;
}
其中
AppSettings.SetValue()和AppSettings.GetValue()方法见:
http://blog.csdn.net/gdjlc/article/details/8284799
SafeCall是个扩展方法
public static void SafeCall(this Control ctrl, Action callback)
{
if (ctrl.InvokeRequired)
ctrl.Invoke(callback);
else
callback();
}
点击【确认】按钮执行结果如下:
正在发送第1个客户...
请等待,暂停 10 秒
正在发送第2个客户...
请等待,暂停 10 秒
正在发送第3个客户...
任务结束
过了3秒钟,点击【确认】按钮并在执行完第一个操作按【取消】执行结果如下:
请等待,暂停 7 秒
正在发送第1个客户...
请等待,暂停 10 秒
正在发送第2个客户...
任务3取消
任务结束
过了5秒钟,点击【确认】按钮执行结果如下:
请等待,暂停 5 秒
正在发送第1个客户...
请等待,暂停 10 秒
正在发送第2个客户...
请等待,暂停 10 秒
正在发送第3个客户...
任务结束
C#定时执行一个操作的更多相关文章
- 使用oracle 的 PL/Sql 定时执行一个存储过程
CSDN日报20170322--<关于软件研发的一些体会总结> 同步博客至 CSDN ,让更多开发者看到你的文章 看微博技术大咖解析互联网应用架构实战 使用oracle 的 PL/Sql ...
- c# 定时启动一个操作、任务
// 定时启动一个操作.任务 using System; using System.Collections.Generic; using System.Collections.ObjectModel; ...
- (原创)在service中定时执行网络操作的几点说明
执行网络操作是耗时操作,即便是在service中也要放到子线程中执行 这里我用到了async-http-client框架来执行异步请求操作 计时用的java原生Timer和TimerTask类 本来这 ...
- Quartz.NET实现作业调度(3.0版本实现)定时执行一个任务
2.0版本请参考https://www.cnblogs.com/best/p/7658573.html这里的文章很详细: 我们现在想每5秒钟往txt文件夹里存储一个时间 首先:定义一个类,实现Quar ...
- 在Windows里定时执行一个Python文件
一.系统环境 操作系统:Win7 64位 二.说明 1.建立一个dos批处理文件 例: @echo off C: cd C:\work\python python aaa.py exit 2.利用Wi ...
- 最简单的???ubuntu 通过crontab定时执行一个程序
crontab在liunx系统中下载,我默认是认为下载安装了的.. crontab貌似只能在liunx系统中存在,如果是windows系统我不知道 创建一个名为jiaoben的文件夹存储sh文件,进入 ...
- oracle定时执行一个存储过程
首先需要新建存储过程 一 存储过程: create or replace procedure Insertdata is begin INSERT INTO tab_dayta select * fr ...
- android 定时执行一个任务
1. timer = new Timer(true) TimerTask task = new TimerTask(){ public void run(){ test(); } } timer.s ...
- java中定时执行任务
现在项目中用到需要定时去检查文件是否更新的功能.timer正好用于此处. 用法很简单,new一个timer,然后写一个timertask的子类即可. 代码如下: package comz.autoup ...
随机推荐
- IOS第18天(8,核心动画转场动画)
***翻页效果 #import "HMViewController.h" @interface HMViewController () @property (weak, nonat ...
- HDU 1264 Counting Squares(线段树求面积的并)
Counting Squares Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- ionic 运用pouchdb/sqlite 数据库做本地存储
配置数据库环境需要3步: 1.安装slqite插件 在ionic 工程目录对应终端执行一下命令: npm install cordova-plugin-sqlite 2.安装pouchdb 在ioni ...
- windows 快捷键
Windows 系统 f6 在同一个应用的不同窗口进行切换 ctrl-shift 拖动,创建文件快捷方式 shift 右键点击文件 可以出现复制路径的菜单 WIN键组合键 Windows Key + ...
- ListView或GridView的Adapter使用Glide加载图片异常
报错信息为:You must not call setTag() on a view Glide is targeting 原因就是View使用setTag后导致Glide之前请求的标记被清除,强制转 ...
- awk 合并文件
问题描述:两个文件a.dat, b.dat a.dat 0 100 1 99 2 93 3 90 ... b.dat 0 0 1 3 2 0 3 2 ... ...
- Java学习-001-JDK安装配置
本节主要讲述在 Win7 64bit 系统下安装.配置 JDK8u25,敬请参阅.详细步骤如下: 一.JDK下载 您可到 官方网站 或 我的云盘 下载,对应的JDK8u25的安装程序,下载过程不再赘述 ...
- 由SecureCRT引发的思考和学习
由SecureCRT引发的思考和学习 http://mp.weixin.qq.com/s?__biz=MzAxOTAzMDEwMA==&mid=2652500597&idx=1& ...
- Android中取消GridView & ListView默认的点击背景色
方法一: gridView.setSelector(new ColorDrawable(Color.TRANSPARENT)); listView.setSelector(new ColorDrawa ...
- Hadoop学习笔记: MapReduce Java编程简介
概述 本文主要基于Hadoop 1.0.0后推出的新Java API为例介绍MapReduce的Java编程模型.新旧API主要区别在于新API(org.apache.hadoop.mapreduce ...