c#实现定时任务(Timer)
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 = ;
public const int SW_HIDE=;
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();
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
//每五分钟执行一次。
//timer.Interval = 300000;
timer.Interval = ;
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)的更多相关文章
- Java基础--定时任务Timer
Java基础--定时任务Timer 一.Timer介绍 java.util.Timer java.util.TimerTask Timer是一个定时器类,通过该类可以为指定的定时任务进行配置.Time ...
- Java基础--定时任务Timer(转载)
Java基础--定时任务Timer 一.Timer介绍 java.util.Timer java.util.TimerTask Timer是一个定时器类,通过该类可以为指定的定时任务进行配置.Time ...
- JAVA定时任务Timer
故事起因 因业务需要,写了一个定时任务Timer,任务将在每天的凌晨2点执行,代码顺利码完,一切就绪,开始测试.运行程序,为了节省时间,将系统时间调整为第二天凌晨1点59分,看着秒针滴答滴答的转动,期 ...
- Java定时任务Timer、TimerTask与ScheduledThreadPoolExecutor详解
定时任务就是在指定时间执行程序,或周期性执行计划任务.Java中实现定时任务的方法有很多,本文从从JDK自带的一些方法来实现定时任务的需求. 一.Timer和TimerTask Timer和Tim ...
- 详解java定时任务---Timer篇
一.简介 在java的jdk中提供了Timer.TimerTask两个类来做定时任务. Timer是一种定时器工具,用来在一个后台线程计划执行指定任务,而TimerTask一个抽象类,它的子 ...
- Java之旅--定时任务(Timer、Quartz、Spring、LinuxCron)
在Java中,实现定时任务有多种方式,本文介绍4种,Timer和TimerTask.Spring.QuartZ.Linux Cron. 以上4种实现定时任务的方式,Timer是最简单的,不需要任何框架 ...
- 服务器启动完成执行定时任务Timer,TimerTask
由于项目需求:每隔一段时间就要调外部接口去进行某些操作,于是在网上找了一些资料,用了半天时间弄好了,代码: import java.util.TimerTask; public class Accou ...
- java定时任务Timer与ScheduledExecutorService<转>
在我们编程过程中如果需要执行一些简单的定时任务,无须做复杂的控制,我们可以考虑使用JDK中的Timer定时任务来实现.下面LZ就其原理.实例以及Timer缺陷三个方面来解析java Timer定时器. ...
- java web定时任务---Timer
写在前面: 在最近的项目中需要每天定时对数据库表进行查询,并完成相关数据的更新操作.首先让我想到的是Timer类,记得在一开始维护那个老系统的时候,开了个接口,也涉及到了定时的操作.下面就记录下大概的 ...
- 定时任务-Timer
Timer类的全限定名 java.util.Timer java.util.Timer类的构造函数 public Timer(); public Timer(boolean isDaemon); pu ...
随机推荐
- Simple Redux
This is a post that tries to explain the the basics of Redux. We’ll build a minimal working example ...
- 7.学习springmvc的异常处理
一.页面的异常处理流程 二.建立项目springmvc04_exception 1.编写index.jsp页面: <%@ page contentType="text/html;cha ...
- SQLSERVER获取数据库中的所有表的名称、表中所有字段的属性
1.查询数据库中的所有数据库名: SELECT Name FROM Master..SysDatabases ORDER BY Name 2.查询某个数据库中所有的表名: SELECT Name FR ...
- 禁用F12,屏蔽鼠标右击、
$(document).ready(function(){ $(document).bind("contextmenu",function(e){ return false; }) ...
- 2018-2019-2 20175211 实验四《Android程序设计》实验报告
目录 一.实验内容及步骤 1.Android Studio的安装测试 2.Activity测试 3.UI测试 4.布局测试 5.事件处理测试 二.问题及解决方法 三.代码托管 四.实验心得体会 一.实 ...
- 有关于服务端模板注入(ssti攻击)——BUUCTF - easy_tornado
打开题目出现3个链接 /flag.txt 中提示flag in /fllllllllllllag /welcome.txt 中提示 render /hints.txt 中提示 md5(cookie_s ...
- 【Gamma】Scrum Meeting 4 & 助教参会记录
目录 前言 任务分配 燃尽图 会议照片 签入记录 上周助教交流总结 技术博客 一些说明 前言 第4次会议于5月29日22:00线上交流形式召开. 交流确认了各自的任务进度,并与助教进行了沟通.时长20 ...
- vue+elementui项目打包后样式变化问题
博主刚刚解决了index.html空白问题,刚打开项目页面又发现了样式出现了大问题,样式与开发版本有很大不同,有些样式没有生效.利用搜索引擎,找到了问题所在以及解决办法:main.js中的引入顺序决定 ...
- js获取form表单所有数据
代码如下: <script type="text/javascript"> // 获取指定form中的所有的<input><select>对象 ...
- 【Docker】容器与系统时间同步
宿主机时间 [root@slave-1 ~]# date Fri May 12 11:20:30 CST 2017 容器时间 [root@slave-1 ~]# docker exec -ti 879 ...