windows服务中的定时器timer的使用
来源:互联网 发布:objectsnmp 源码下载 编辑:程序博客网 时间:2024/05/16 09:42
大家在使用c#写windows服务时,网上很多例子说,使用工具箱中组件中的定时器就可以,但实际上该定时器并不管用,具体原因,微软自称是Framework的bug。
无论是windows窗体中timer还是组件中的timer都是继承自System.Windows.Form.Timer,并不是我们需要的System.Timers ,继承自System.Timers 的timer只有手动创建。
}
=========================================================================
双方 该定时器要避免的是 使用类似下面的例子 如果您选择 这两个例子来自MSDN的网页。System.Timers.Timer
和System.Threading.Timer
将工作服务。System.Web.UI.Timer
和System.Windows.Forms.Timer
这是ASP应用程序和WinForms分别。 使用这些将导致服务加载的额外组装这不是真正的应用程序,您需要的是建筑类型。System.Timers.Timer
另外,请确保您使用一个类级变量,以防止垃圾收集):using System;
using System.Timers;
public class Timer1
{
private static System.Timers.Timer aTimer;
public static void Main()
{
// 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. (See end of method.)
//System.Timers.Timer aTimer;
// Create a timer with a ten second interval.
aTimer = new System.Timers.Timer(10000);
// Hook up the Elapsed event for the timer.
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
// Set the Interval to 2 seconds (2000 milliseconds).
aTimer.Interval = 2000;
aTimer.Enabled = true;
Console.WriteLine("Press the Enter key to exit the program.");
Console.ReadLine();
// If the timer is declared in a long-running method, use
// KeepAlive to prevent garbage collection from occurring
// before the method ends.
//GC.KeepAlive(aTimer);
}
// Specify what you want to happen when the Elapsed event is
// raised.
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);
}
}
/* This code example produces output similar to the following:
Press the Enter key to exit the program.
The Elapsed event was raised at 5/20/2007 8:42:27 PM
The Elapsed event was raised at 5/20/2007 8:42:29 PM
The Elapsed event was raised at 5/20/2007 8:42:31 PM
...
*/ System.Threading.Timer
可以使用如下:using System;
using System.Threading;
class TimerExample
{
static void Main()
{
AutoResetEvent autoEvent = new AutoResetEvent(false);
StatusChecker statusChecker = new StatusChecker(10);
// Create the delegate that invokes methods for the timer.
TimerCallback timerDelegate =
new TimerCallback(statusChecker.CheckStatus);
// Create a timer that signals the delegate to invoke
// CheckStatus after one second, and every 1/4 second
// thereafter.
Console.WriteLine("{0} Creating timer./n",
DateTime.Now.ToString("h:mm:ss.fff"));
Timer stateTimer =
new Timer(timerDelegate, autoEvent, 1000, 250);
// When autoEvent signals, change the period to every
// 1/2 second.
autoEvent.WaitOne(5000, false);
stateTimer.Change(0, 500);
Console.WriteLine("/nChanging period./n");
// When autoEvent signals the second time, dispose of
// the timer.
autoEvent.WaitOne(5000, false);
stateTimer.Dispose();
Console.WriteLine("/nDestroying timer.");
}
}
class StatusChecker
{
int invokeCount, maxCount;
public StatusChecker(int count)
{
invokeCount = 0;
maxCount = count;
}
// This method is called by the timer delegate.
public void CheckStatus(Object stateInfo)
{
AutoResetEvent autoEvent = (AutoResetEvent)stateInfo;
Console.WriteLine("{0} Checking status {1,2}.",
DateTime.Now.ToString("h:mm:ss.fff"),
(++invokeCount).ToString());
if(invokeCount == maxCount)
{
// Reset the counter and signal Main.
invokeCount = 0;
autoEvent.Set();
}
}
}
- windows服务中的定时器timer的使用
- windows服务中的定时器timer的使用 写日志
- Windows中定时器Timer使用中的注意事项
- C#windows服务中的Timer控件的使用
- C#创建windows服务搭配定时器Timer使用实例
- gevent中的定时器 timer的简单使用
- windows服务中的Timer
- C# windows服务:C#windows服务中的Timer控件的使用
- timer 定时器的使用
- Timer定时器的使用
- 定时器Timer的使用
- Timer 定时器的使用
- timer定时器的使用
- Timer定时器的使用
- Timer定时器的使用
- java中的定时器Timer的使用及实例
- java中的定时器Timer的使用及实例
- Android中的定时器Timer、AlarmManager、CountDownTimer的使用
- 算法导论--合并算法
- PE文件格式”1.9版 完整译文(附注释)
- 关于创业合作方面的认识(thinkvd开发日志)
- java按字节截取字符串
- Windows7安装包中打开WIM文件并提取系统文件
- windows服务中的定时器timer的使用
- 关于奇异值分解的一些讨论
- CHK恢复软件,CHK恢复工具-CHKRecovery V2.10 最新版上线!
- pylucene入门
- 用GridView实现无限级嵌套
- java
- Subversion合并跟踪 – 基础
- 在fedora 10下载android源码
- Java程序执行超时——Future接口介绍