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 ...
随机推荐
- 解决node.js链接数据库时出现的报错 --- client does not support authentication
打开mysql数据库小黑屏 然后输入 mysql> alter user 'root'@'localhost' identified with mysql_native_password by ...
- Nuxt项目中通过ESlint命令行修复格式问题
在package.json文件中, 往往会有个类似于 "lint": "eslint --ext .js,.vue --ignore-path .gitignore .& ...
- [Git] Remove Files from Staging Before Committing
If you've added files to the staging area (the Index) accidentally - you can remove them using git r ...
- LeetCode 422. Valid Word Square
原题链接在这里:https://leetcode.com/problems/valid-word-square/ 题目: Given a sequence of words, check whethe ...
- svn命令(转)
参考资料: 1. http://os.51cto.com/art/200908/143157_all.htm2. https://www.jianshu.com/p/d3ebfa27b3ba3. ht ...
- 在没有符号和FPO的情况下遍历堆栈(帧指针省略)
下面是应用程序崩溃转储的调用堆栈.报告的崩溃是名为“HelperLibrary”的模块内的访问冲突,我们没有该模块的符号或源代码.调用堆栈看起来不太可能: 0:000> kv ChildEBP ...
- shell 判断操作系统
#!/bin/sh a=`uname -a` b="Darwin" c="centos" d="ubuntu" if [[ $a =~ $b ...
- MySQL避免插入重复记录:唯一性约束
mysql在存在主键冲突或者唯一键冲突的情况下,根据插入策略不同,一般有以下三种避免方法.1.insert ignore2.replace into3.insert on duplicate ke ...
- Shell字符串截取(非常详细)
假设有变量 var=http://www.aaa.com/123.htm. 1. # 号截取,删除左边字符,保留右边字符. 1 echo ${var#*//} 其中 var 是变量名,# 号是运算符, ...
- 颜色空间模型(HSV\LAB\RGB\CMYK)
通过Photoshop的拾色器,我们知道表征颜色的模型的不止一种,本文将系统并且详细讨论这四种模型(HSV.LAB.RGB和CMYK)之间的联系以及应用.本文部分章节整合了多位优秀博主的博客(链接见本 ...