背景:
最近在做一个项目,程序是命令行程序,在主程序中开一个线程,这个线程用到了System.Timer类的Elapsed事件,根据指定时间间隔循环去查询数据库,找符合条件的记录,把记录组织成xml对象发送到MSMQ中去。刚一开始的时候数据量小,在时间间隔内可以查询所有的记录并发送到MSMQ,随着业务量大增大,在时间间隔内会多次执行查询数据库发送MSMQ,这样就会产生重复的数据发送到MSMQ了。所以本次在System.Timer类的Elapsed事件中加锁,使上次任务执行完之前,后面的任务无法执行。

程序代码:

    public class ThreadSendMQ
{
public void Work(object th)
{
string str = "消息队列发送模式:多线程发送模式。";
Console.WriteLine(str); System.Timers.Timer t = new System.Timers.Timer();
t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);
t.Interval = * Convert.ToInt16(ConfigManager.SleepTime);
t.Enabled = true;
} void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
//加锁检查数据库和发送MSMQ
lock (this)
{
send();
}
} private void send()
{
string str = "线程正在发送消息队列";
//线程查询数据库并发送到MSMQ中去。
//Console.Write(str);
//LogManager.WriteFilerLog(ConfigManager.LogPath, str);
}
} /////// 主程序 //////////////
class Program
{
static void Main(string[] args)
{
ThreadSendMQ thSend = new ThreadSendMQ();
System.Threading.ParameterizedThreadStart thrParm = new System.Threading.ParameterizedThreadStart(thSend.Work);
System.Threading.Thread thread = new System.Threading.Thread(thrParm);
thread.Start(System.Threading.Thread.CurrentThread);
}
}

经过上面的锁,就可以防止任务的多次执行,当前项目的bug完美解决。

给System.Timer类的Elapsed事件加锁的更多相关文章

  1. C#中timer类的用法

    C#中timer类的用法 关于C#中timer类  在C#里关于定时器类就有3个   1.定义在System.Windows.Forms里   2.定义在System.Threading.Timer类 ...

  2. 使用System.Timers.Timer类实现程序定时执行

    使用System.Timers.Timer类实现程序定时执行 在C#里关于定时器类有3个:System.Windows.Forms.Timer类.System.Threading.Timer类和Sys ...

  3. 定时器:Timer:System.Threading.Timer类(转)

    最近的一个项目有一些地方需要用到定时功能,在设计过程中,突然发现.net的Timer类居然还有很多我以前没有用过的功能,这里就跟大家分享一下 注:这里的Timer类特指System.Threading ...

  4. C#的timer类

    在C#里关于定时器类就有3个 1.定义在System.Windows.Forms里 2.定义在System.Threading.Timer类里 3.定义在System.Timers.Timer类里 S ...

  5. 关于C#中timer类

    ·关于C#中timer类 在C#里关于定时器类就有3个 1.定义在System.Windows.Forms里 2.定义在System.Threading.Timer类里 3.定义在System.Tim ...

  6. 使用timer定时器,防止事件重入

    首先简单介绍一下timer,这里所说的timer是指的System.Timers.timer,顾名思义,就是可以在指定的间隔是引发事件.官方介绍在这里,摘抄如下: 1 2 Timer 组件是基于服务器 ...

  7. Timer类的常见使用方法

    System.Timers名称空间中的Timer类的构造函数只需要一个时间间隔,经过该时间间隔后应该调用的方法用Elapsed事件指定,这个事件需要一个ElapsedEventHandler类型的委托 ...

  8. java swing中Timer类的学习

    最近在完成学校课程的java平时作业,要实现一个计时器,包含开始.暂停以及重置三个功能.由于老师规定要用这个timer类,也就去学习了一下,顺便记录一下. 首先呢去查了一下java手册上的东西,发现t ...

  9. 适配器、工厂模式、线程池、线程组、互斥锁、Timer类、Runtime类、单例设计模式(二十四)

    1.多线程方法 * Thread 里面的俩个方法* 1.yield让出CPU,又称为礼让线程* 2.setPriority()设置线程的优先级 * 优先级最大是10,Thread.MAX_PRIORI ...

随机推荐

  1. sqlmap的简要使用方法

    仅作为备忘!!! sqlmap使用: 注入类型有四种分别为:boolean-based blind.error-based.stacked queries.inline query. sqlmap - ...

  2. 通过代码来调用log4net写日志

    1.使用如下配置 http://www.cnblogs.com/chucklu/p/5404813.html 2.调用 string ConfigFile; private void LoadLogC ...

  3. ThinkPHP3.2添加scws中文分词

    前言 前一段时间,公司网站做站内搜索,只简单针对输入的文字进行搜索,作全匹配检索,搜索出来的内容很少.如何达到模糊搜索,匹配到更多的内容成了需要解决的问题.于是,今天想到可以做分词检索,如何对输入的一 ...

  4. static变量的作用

    在C语言中,关键字static的意思是静态的,有3个明显的作用: 1. 在函数体内,静态变量具有记忆作用,即一个被声明为静态的变量在这一函数被调用的过程中其值维持不变. 2. 在模块内(但在函数体外) ...

  5. Pandas索引和选择数据

    在本章中,我们将讨论如何切割和丢弃日期,并获取Pandas中大对象的子集. Python和NumPy索引运算符"[]"和属性运算符".". 可以在广泛的用例中快 ...

  6. scala学习手记2 - scala中的循环

    先来看一段Java中的循环: for (int i = 1; i < 4; i++) { System.out.print(i + ","); } 毫无疑问,scala可以让 ...

  7. 报错org.apache.ibatis.binding.BindingException: Type interface com.atguigu.mybatis.bean.dao.EmployeeMapper is not known to the MapperRegistry.

    报错org.apache.ibatis.binding.BindingException: Type interface com.atguigu.mybatis.bean.dao.EmployeeMa ...

  8. combo

    什么是combo 上一节中我们有一行代码: <script src="http://g.tbcdn.cn/kissy/k/1.4.8/??seed-min.js,import-styl ...

  9. Linux软件安装-----apache安装

    一. ./configure --prefix=/usr/local/http2 \ --enable-modules=all \ --enble-mods-shared=all \ 开启模块共享: ...

  10. spring3: 4.4 使用路径通配符加载Resource

    4.4.1  使用路径通配符加载Resource 前面介绍的资源路径都是非常简单的一个路径匹配一个资源,Spring还提供了一种更强大的Ant模式通配符匹配,从能一个路径匹配一批资源. Ant路径通配 ...