博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c#实现定时任务(Timer)
阅读量:5813 次
发布时间:2019-06-18

本文共 2206 字,大约阅读时间需要 7 分钟。

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;using System.Runtime.InteropServices; namespace AutoCreateTaskApp{    class Program    {        [DllImport("user32.dll")]        public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);        public  const int SW_SHOWMINIMIZED = 2;        public const int SW_HIDE=0;        static IntPtr winHandle = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;            static void Main(string[] args)        {            //使控制台在后台执行。            ShowWindow(winHandle, SW_HIDE);            // Normally, the timer is declared at the class level,            // so that it stays in scope as long as it is needed.            // If the timer is declared in a long-running method,              // KeepAlive must be used to prevent the JIT compiler             // from allowing aggressive garbage collection to occur             // before the method ends. You can experiment with this            // by commenting out the class-level declaration and             // uncommenting the declaration below; then uncomment            // the GC.KeepAlive(aTimer) at the end of the method.            //timer = new System.Timers.Timer();             System.Timers.Timer timer = new System.Timers.Timer(1000);            timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);            //每五分钟执行一次。            //timer.Interval = 300000;              timer.Interval = 2000;            timer.Enabled = true;            // If the timer is declared in a long-running method, use            // KeepAlive to prevent garbage collection from occurring            // before the method ends.            GC.KeepAlive(timer);            //指定无限长等待时间的常数            Thread.Sleep(System.Threading.Timeout.Infinite);        }        static void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)        {            Console.WriteLine(DateTime.Now.ToString());            Helper.AutoCreateTask();         }       }}--------------------- 作者:arvinstudy 来源:CSDN 原文:https://blog.csdn.net/ArvinStudy/article/details/7374888 版权声明:本文为博主原创文章,转载请附上博文链接!

 

转载于:https://www.cnblogs.com/webenh/p/11007491.html

你可能感兴趣的文章
C#泛型-什么是泛型
查看>>
regsvr32.exe进程注册dll文件
查看>>
C# 单机Window 程序 sqlite 数据库实现
查看>>
JavaScript一些函数
查看>>
国务院关于积极推进“互联网+”行动的指导意见
查看>>
Matrix Factorization, Algorithms, Applications, and Avaliable packages
查看>>
图像配准转换
查看>>
mysql-This version of MySQL doesn’t yet support ‘LIMIT & IN/ALL/ANY/SOME 错误解决
查看>>
BIEE Demo(RPD创建 + 分析 +仪表盘 )
查看>>
Cocos2dx 3.0开发环境的搭建--Eclipse建立在Android工程
查看>>
iOS人脸识别核心代码(备用)
查看>>
基本概念复习
查看>>
C++:成员运算符重载函数和友元运算符重载函数的比较
查看>>
[Polymer] Introduction
查看>>
【iCore3 双核心板】例程三十:U_DISK_IAP_FPGA实验——更新升级FPGA
查看>>
前端技术的发展和趋势
查看>>
Android文件下载之进度检测
查看>>
重构第10天:提取方法(Extract Method)
查看>>
吐血整理 Delphi系列书籍 118本(全)
查看>>
Android Fragment使用(四) Toolbar使用及Fragment中的Toolbar处理
查看>>