.net – 在Windows服务中使用计时器
发布时间:2020-07-09 01:50:56 所属栏目:Windows 来源:互联网
导读:我有一个Windows服务,我想要每10秒创建一个文件。 我得到了很多评论,在Windows服务中的计时器将是最好的选择。 我该如何实现? 首先选择正确的计时器。您需要System.Timers.Timer或System.Threading.Timer – 不要使用与UI框架相关联的一个(例如System.Wind
|
我有一个Windows服务,我想要每10秒创建一个文件。 我得到了很多评论,在Windows服务中的计时器将是最好的选择。 我该如何实现? 首先选择正确的计时器。您需要System.Timers.Timer或System.Threading.Timer – 不要使用与UI框架相关联的一个(例如System.Windows.Forms.Timer或DispatcherTimer)。计时器一般简单 >设置刻度间隔 一切都会好起来的。 样品: // System.Threading.Timer sample
using System;
using System.Threading;
class Test
{
static void Main()
{
TimerCallback callback = PerformTimerOperation;
Timer timer = new Timer(callback);
timer.Change(TimeSpan.Zero,TimeSpan.FromSeconds(1));
// Let the timer run for 10 seconds before the main
// thread exits and the process terminates
Thread.Sleep(10000);
}
static void PerformTimerOperation(object state)
{
Console.WriteLine("Timer ticked...");
}
}
// System.Timers.Timer example
using System;
using System.Threading;
using System.Timers;
// Disambiguate the meaning of "Timer"
using Timer = System.Timers.Timer;
class Test
{
static void Main()
{
Timer timer = new Timer();
timer.Elapsed += PerformTimerOperation;
timer.Interval = TimeSpan.FromSeconds(1).TotalMilliseconds;
timer.Start();
// Let the timer run for 10 seconds before the main
// thread exits and the process terminates
Thread.Sleep(10000);
}
static void PerformTimerOperation(object sender,ElapsedEventArgs e)
{
Console.WriteLine("Timer ticked...");
}
}
我有更多的信息this page,虽然我没有更新很长一段时间。 (编辑:日照站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 使用Windows Power Management API调暗监视器
- node-ffi模块的安装以及基于electron生成windows桌面应用程
- Microsoft Windows远程桌面协议中间人攻击漏洞(CVE-2005-17
- win10 安装msi 提示2502、2503的错误代码(已成功解决)
- windows – sysopen权限被拒绝
- 2018-5-26 怎么在windows上远程连接linux服务器上的mysql
- Windows – LogonUser – 来自系统服务的CreateProcessAsUs
- Windows Concole中的希腊字母
- 使用def文件简化dll导出
- win10 uwp 商业游戏 1.2.1
推荐文章
站长推荐
- windows – 为什么自定义光标图像显示不正确?
- win7运行mapreduce报错Could not locate executa
- Windows域用户设置用户登录脚本
- windows-mobile – Visual Studio 2010 Professi
- windows-nginx-https-本地配置
- windows7下composer安装不了或composer命令无效的
- win10家庭版 远程桌面 身份验证错误,要求的函数
- 获取与Windows Vista上的C#.Net连接的无线网络的
- windows – 在VBScript中获取命令行输出(无需写入
- windows-services – 监控单个窗口服务的性能
热点阅读
