.net – 在Windows服务中使用计时器
发布时间:2020-07-09 01:46:22 所属栏目: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 – 我可以将16位.exe程序转换为64位.exe吗?
- npm ERR!注册表错误解析json – 尝试在Windows 8中安装Cor
- Windows:忘记本地账户开机密码,但记得住PIN码
- windows – 包含带空格的可执行路径的环境变量是否也包含必
- windows – 如何使用Win32 API获取多个监视器的显示名称?
- 找出占用Installer 目录空间的元凶
- Windows – 如何将多个文件名传递给上下文菜单Shell命令?
- npm及cnpm(Windows)基础环境搭建
- windows-phone-7 – Caliburn Micro中的墓碑
- winapi – 我可以从DLL中删除数字签名吗?
推荐文章
站长推荐
- ms-office – Microsoft Office 2010功能区自定义
- Vagrant系列(一)----win10搭建Vagrant+VirtualBo
- winapi – Windows SDK 7.0的signtool中的signwi
- macos – 在Windows上使用Tycho构建的Eclipse RC
- Windows 8 – 低延迟音频
- 我们可以在Microsoft Bot Framework(.NET / C#)中
- 使用免费工具开发轻量级(无运行时)基于Windows的
- Windows 下安装 Node.js
- windows-services – 监控单个窗口服务的性能
- xaml – 在开发Windows 8 Store应用程序时处理不
热点阅读
