定时任务-C#线程类 windows服务

原理

最常用的就是C#中 timer类写一个定时方法,然后在把他宿主到windows服务里面。

C#中Timer分类

关于C# Timer类  在C#里关于定时器类就有3个

C# Timer使用的方法1.定义在System.Windows.Forms里

C# Timer使用的方法2.定义在System.Threading.Timer类里  "

C# Timer使用的方法3.定义在System.Timers.Timer类里

◆System.Windows.Forms.Timer

应用于WinForm中的,它是通过Windows消息机制实现的,类似于VB或Delphi中的Timer控件,内部使用API  SetTimer实现的。它的主要缺点是计时不精确,而且必须有消息循环,Console  Application(控制台应用程序)无法使用。   
  
◆System.Timers.Timer

和System.Threading.Timer非常类似,它们是通过.NET  Thread  Pool实现的,轻量,计时精确,对应用程序、消息没有特别的要求。

◆System.Timers.Timer还可以应用于WinForm,完全取代上面的Timer控件。它们的缺点是不支持直接的拖放,需要手工编码。

System.Threading.Timer

public class BizCommon
{
    /// <summary>
    ////// </summary>
    public static object LockObject = new object();

    public static void StartTime()
    {
        //第二个参数是回调方法的参数
        Timer t = new Timer(StartBiz, null, 0, 5000);
        //t.Change(0, 5000);
    }

    private static void StartBiz(object o)
    {
        if (Monitor.TryEnter(LockObject))
        {
            FileStream fs = new FileStream("C:\\log.txt", FileMode.OpenOrCreate);
            StreamWriter sw = new StreamWriter(fs);
            sw.WriteLine("Time:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff"));
            sw.Close();
        }
    }
}
View Code

System.Timers.Timer

public class BizCommon
{
    /// <summary>
    ////// </summary>
    public static object LockObject = new object();

    public static void StartTime()
    {
        System.Timers.Timer tm = new System.Timers.Timer();
        tm.Interval = 5000;
        tm.Elapsed += new System.Timers.ElapsedEventHandler(StartBiz);
        tm.AutoReset = true; //执行一次false,一直循环执行true
        tm.Enabled = true;//是否执行Elapsed事件。
        tm.Start();
        //tm.Stop();
    }
    private static void StartBiz(object sender, System.Timers.ElapsedEventArgs e)
    {
        if (Monitor.TryEnter(LockObject))
        {
            FileStream fs = new FileStream("C:\\log.txt", FileMode.OpenOrCreate);
            StreamWriter sw = new StreamWriter(fs);
            sw.WriteLine("Time:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff"));
            sw.Close();
        }
    }
}
View Code

更多关于多线程的教程请看

http://www.cnblogs.com/wudequn/category/1154929.html

 

public static void StartTime()
{
    System.Timers.Timer tm = new System.Timers.Timer();
    tm.Interval = 1000;
    tm.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Elapsed);
    tm.Start();

}
private static void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
    int intHour = e.SignalTime.Hour;
    int intMinute = e.SignalTime.Minute;
    int intSecond = e.SignalTime.Second;
    // 设置 每天 00:00:00开始执行程序  
    //
    //由于计算机性能问题,有时候可能会跳过某一秒,导致没有执行。  这种导致任务没有处理的异常叫做missfire
    //这种就要做很多处理,其他时间段在查看数据库是否今天这个时间处理过。没有处理在重新处理等等 补偿操作。
    if (intHour == 00 && intMinute == 00 && intSecond == 00)
    {
        //do something
    }
}
特定时间点处理任务
Windows服务

创建过程

 

下载项目

编译完之后,里面有安装 和 写在 windows服务的批处理文件。

 

转载于:https://www.cnblogs.com/wudequn/p/8353320.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以通过在 Windows Service 的 OnStart 方法中创建一个新的线程来实现定时每天1点执行任务。下面是一些实现步骤: 1. 创建一个 Windows Service 项目。 2. 在 Service1.cs 文件中,重写 OnStart 和 OnStop 方法。 3. 在 OnStart 方法中,创建一个新的线程,并在其中开启一个无限循环,每隔一段时间检查当前时间,并在1点时执行任务。 4. 在无限循环中,通过 Thread.Sleep 方法来控制检查时间的间隔。 下面是一个示例代码: ```csharp public partial class Service1 : ServiceBase { private Thread thread; private bool isRunning; public Service1() { InitializeComponent(); } protected override void OnStart(string[] args) { // 创建新线程并启动 thread = new Thread(new ThreadStart(DoWork)); thread.Start(); } protected override void OnStop() { // 停止线程 isRunning = false; if (thread != null) { thread.Join(); } } private void DoWork() { isRunning = true; while (isRunning) { // 获取当前时间 DateTime now = DateTime.Now; // 如果当前时间是1点,则执行任务 if (now.Hour == 1 && now.Minute == 0 && now.Second == 0) { // 执行任务的代码 // TODO: 在此处编写你的任务代码 } // 等待一段时间后再次检查 Thread.Sleep(1000); // 1秒钟 } } } ``` 需要注意的是,由于线程是在 OnStart 方法中创建的,因此需要在 OnStop 方法中停止线程。在停止线程之前,需要将 isRunning 设置为 false,以便让线程退出无限循环。此外,线程在执行任务时可能会占用较高的 CPU 和内存资源,因此需要根据实际情况来设置检查时间的间隔。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值