首页 / C# / c#实现定时任务(Timer)
c#实现定时任务(Timer)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c#实现定时任务(Timer),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1807字,纯文字阅读大概需要3分钟。
内容图文
![c#实现定时任务(Timer)](/upload/InfoBanner/zyjiaocheng/804/7b28cf5115d44e77876fceb82c01bc7e.jpg)
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 版权声明:本文为博主原创文章,转载请附上博文链接!
内容总结
以上是互联网集市为您收集整理的c#实现定时任务(Timer)全部内容,希望文章能够帮你解决c#实现定时任务(Timer)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。