• 大小: 9.28MB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2023-11-18
  • 语言: C#
  • 标签: 1.0  

资源简介

调度实现定时任务,定时实现某些业务,可以自由选择调度重复次数。

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using Topshelf;

namespace TimerAndDispatch
{
    class Program
    {
        #region Timer定时任务

        static void Main1(string[] args)
        {
            System.Timers.Timer timer = new System.Timers.Timer();
            timer.Enabled = true;
            timer.Interval = 3000; //执行间隔时间单位为毫秒; 这里实际间隔为3秒 
            timer.Start();
            timer.Elapsed += new System.Timers.ElapsedEventHandler(test1);

            Console.ReadKey();
        }

        private static void test1(object source ElapsedEventArgs e)
        {

            Console.WriteLine(“OK test event is fired at: “ + DateTime.Now.ToString());

        }

        static void Main2(string[] args)
        {
            System.Timers.Timer timer = new System.Timers.Timer();
            timer.Enabled = true;
            timer.Interval = 1000;//执行间隔时间单位为毫秒;此时时间间隔为1分钟  
            timer.Start();
            timer.Elapsed += new System.Timers.ElapsedEventHandler(test2);

            Console.ReadKey();
        }

        private static void test2(object source ElapsedEventArgs e)
        {

            if (DateTime.Now.Second == 30)  //如果当前时间是30秒
                Console.WriteLine(“OK event fired at: “ + DateTime.Now.ToString());

        }
        #endregion


        #region 任务调度

        static void Main(string[] args)
        {

            //NLog.ILogger logger = NLog.LogManager.GetCurrentClassLogger();
            //logger.Error(“111111“);

            // 配置和运行宿主服务
            HostFactory.Run(x =>
            {
                // 指定服务类型。这里设置为 TestSchedule
                x.Service(s =>
                {
                    // 通过 new TestSchedule() 构建一个服务实例 
                    s.ConstructUsing(name => new TestSchedule());
                    // 当服务启动后执行什么
                    s.WhenStarted(tc => tc.Start());
                    // 当服务停止后执行什么
                    s.WhenStopped(tc => tc.Stop());
                });

                // 服务用本地系统账号来运行
                x.RunAsLocalSystem();

                // 服务描述信息
                x.SetDescription(“TestSchedule“);
                // 服务显示名称
                x.SetDisplayName(“TestScheduleService“);
                // 服务名称
                x.SetServiceName(“TestScheduleService“);

            });


        }

        #endregion
    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

    ..A..H.     74240  2019-01-15 10:30  TimerAndDispatch\.vs\TimerAndDispatch\v15\.suo

     文件     778240  2019-01-15 10:18  TimerAndDispatch\.vs\TimerAndDispatch\v15\sqlite3\storage.ide

     文件       9490  2018-11-06 09:32  TimerAndDispatch\packages\NLog.4.5.11\.signature.p7s

     文件     621056  2018-11-06 17:07  TimerAndDispatch\packages\NLog.4.5.11\lib\monoandroid44\NLog.dll

     文件    1355032  2018-11-06 17:07  TimerAndDispatch\packages\NLog.4.5.11\lib\monoandroid44\NLog.xml

     文件     688128  2018-11-06 17:07  TimerAndDispatch\packages\NLog.4.5.11\lib\net35\NLog.dll

     文件    1490624  2018-11-06 17:07  TimerAndDispatch\packages\NLog.4.5.11\lib\net35\NLog.xml

     文件     695296  2018-11-06 17:07  TimerAndDispatch\packages\NLog.4.5.11\lib\net40-client\NLog.dll

     文件    1495274  2018-11-06 17:07  TimerAndDispatch\packages\NLog.4.5.11\lib\net40-client\NLog.xml

     文件     704000  2018-11-06 17:07  TimerAndDispatch\packages\NLog.4.5.11\lib\net45\NLog.dll

     文件    1510833  2018-11-06 17:07  TimerAndDispatch\packages\NLog.4.5.11\lib\net45\NLog.xml

     文件     603136  2018-11-06 17:07  TimerAndDispatch\packages\NLog.4.5.11\lib\netstandard1.3\NLog.dll

     文件    1317777  2018-11-06 17:07  TimerAndDispatch\packages\NLog.4.5.11\lib\netstandard1.3\NLog.xml

     文件     635392  2018-11-06 17:07  TimerAndDispatch\packages\NLog.4.5.11\lib\netstandard1.5\NLog.dll

     文件    1361180  2018-11-06 17:07  TimerAndDispatch\packages\NLog.4.5.11\lib\netstandard1.5\NLog.xml

     文件     654848  2018-11-06 17:07  TimerAndDispatch\packages\NLog.4.5.11\lib\netstandard2.0\NLog.dll

     文件    1401653  2018-11-06 17:07  TimerAndDispatch\packages\NLog.4.5.11\lib\netstandard2.0\NLog.xml

     文件     556032  2018-11-06 17:07  TimerAndDispatch\packages\NLog.4.5.11\lib\sl4\NLog.dll

     文件    1085995  2018-11-06 17:07  TimerAndDispatch\packages\NLog.4.5.11\lib\sl4\NLog.xml

     文件     560640  2018-11-06 17:07  TimerAndDispatch\packages\NLog.4.5.11\lib\sl5\NLog.dll

     文件    1090250  2018-11-06 17:07  TimerAndDispatch\packages\NLog.4.5.11\lib\sl5\NLog.xml

     文件     543744  2018-11-06 17:07  TimerAndDispatch\packages\NLog.4.5.11\lib\wp8\NLog.dll

     文件    1038453  2018-11-06 17:07  TimerAndDispatch\packages\NLog.4.5.11\lib\wp8\NLog.xml

     文件     619008  2018-11-06 17:07  TimerAndDispatch\packages\NLog.4.5.11\lib\xamarinios10\NLog.dll

     文件    1350647  2018-11-06 17:07  TimerAndDispatch\packages\NLog.4.5.11\lib\xamarinios10\NLog.xml

     文件    3791407  2019-01-15 09:45  TimerAndDispatch\packages\NLog.4.5.11\NLog.4.5.11.nupkg

     文件       9461  2018-10-07 05:31  TimerAndDispatch\packages\Quartz.3.0.7\.signature.p7s

     文件     866816  2018-10-07 12:27  TimerAndDispatch\packages\Quartz.3.0.7\lib\net452\Quartz.dll

     文件     225172  2018-10-07 12:27  TimerAndDispatch\packages\Quartz.3.0.7\lib\net452\Quartz.pdb

     文件    1040642  2018-10-07 12:27  TimerAndDispatch\packages\Quartz.3.0.7\lib\net452\Quartz.xml

............此处省略81个文件信息

评论

共有 条评论