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 ...
随机推荐
- [React] Write a generic React Suspense Resource factory
Using Suspense within our component isn't exactly ergonomic. Let's put all that logic into a reusabl ...
- [Algorithm] 94. Binary Tree Inorder Traversal iteratively approach
Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] ...
- CLR Exception---E0434352
什么是CLR Exception---E0434352 CLR异常是.NET应用程序生成的异常类型.异常被封装在从System.exception类派生的类中.它的异常代码是0xE0434352,代码 ...
- K8s Service原理介绍
Service的工作方式有三种: 第一种: 是Userspace方式 如下图描述, Client Pod要访问Server Pod时,它先将请求发给本机内核空间中的service规则,由它再将请求, ...
- pycharm配置默认代码和注释模板
有人问,在pycharm新建python文件时,文件开头的注释每次都要重复写,能不能配置成模板?其实pycharm本身自带此功能 在pycharm菜单栏找File -> settings -&g ...
- Mongoose 内置 CURD 方 法、扩展 Mongoose Model 的静态方法和 实例方法
Mongoose 内置 CURD 方 法 Mongoose 内置 CURD 方 法文档地址:https://mongoosejs.com/docs/queries.html 常用的方法如下: Mode ...
- webpack4.0中文文档踩坑记录
一直没有正儿八经去看过webpack4.0的文档,前段时间工作比较轻松,于是就有了此文...面都这样一个问题:请问在您的开发生涯中,令你最痛苦最无奈的是什么?小生的回答只有一个:“阅读那些令人发指的文 ...
- web程序设计关于我们
项目名称 福大咸鱼市场 开发团队 项目板块 负责人 美工 黄鸿杰 后端 胡继文 前端 葛家灿 联系方式:1175204449@qq.com
- Spark2.x(六十二):(Spark2.4)共享变量 - Broadcast原理分析
之前对Broadcast有分析,但是不够深入<Spark2.3(四十三):Spark Broadcast总结>,本章对其实现过程以及原理进行分析. 带着以下几个问题去写本篇文章: 1)dr ...
- tensorflow keras analysis
目录 Q: where is Sequential defined? Q: where is compile()? tensorflow keras analysis code from keras. ...