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网络第二天 - 01-基本的HTTP请求
***************** #import "HMViewController.h" #import "MBProgressHUD+MJ.h" @int ...
- JVM性能监控与故障处理命令行工具
JDK命令行工具 Sun公司作为”礼物“赠送给JDK使用者的工具: 这些命令行工具大多是jdk/lib/tools.jar类库的一层薄包装,主要功能代码是在tools类库(不属于java的标准API) ...
- ng-repeat 嵌套 ng-switch 出错解决
Error: $compile:ctreq Missing Required Controller Controller 'ngSwitch', required by directive 'ngSw ...
- iOS cocospods Updating local specs repositories
pod install --verbose --no-repo-update (在安装的时候) pod update --verbose --no-repo-update (在更新库的时候) 如果长时 ...
- C++程序设计(三)
1. 运算符重载 目的:对抽象数据类型也能够直接使用C++提供的运算符.使得程序更简洁,代码更容易理解. 运算符重载的实质是函数重载 返回值类型 operator 运算符(形参表) { -- } 运算 ...
- iOS后台定位,实时向服务器发送最新位置
第一步,开启后台模式,选中定位,选择project --> capabilities-->Backgorund Modes --> Location updates 如图: Past ...
- MacOS10.11的/usr/bin目录不可写后class-dump的处理办法
许多升级了OSX 10.11的朋友在配置class-dump的时候,会发现书上推荐的class-dump存放目录/usr/bin不再可写,如下所示: 192:~ snakeninny$ touch c ...
- CentOS修改mysql 用户root的密码并允许远程登录
第一步:用帐号登录mysql[root@CentOs5 ~]# mysql -u root -p 第二步:改变用户数据库mysql> use mysql 第三步:修改密码,记得密码要用passw ...
- (copy) Shell Script to Check Linux System Health
source: http://linoxide.com/linux-shell-script/shell-script-check-linux-system-health/ This article ...
- 图割Graph-Cut的最大流实现
利用最大流标号法求解最大流,详见代码: Version:未加头尾节点版: 缺点:havn't take nodes' pixels into consideration /************** ...