第一个Windows服务 - 计时器似乎没有打勾

Kov*_*ovu 3 .net c# windows-services

我写了我的第一个Windows服务.

  1. 创建WS项目
  2. 重命名服务
  3. 将计时器拖到中间
  4. 启用它,勾选为1
  5. 不存在时,在刻度线中创建一个logfie
  6. 安装该服务
  7. 运行服务

什么都没发生...

我尝试连接到服务,它已正确加载,但有一个断点,它永远不会打.

有任何想法吗?


代码定时器:

private void timMain_Tick(object sender, EventArgs e)
{
    if (!File.Exists("C:/test.txt"))
    File.Create("C:/test.txt");
}
Run Code Online (Sandbox Code Playgroud)

代码初始化:

private void InitializeComponent()
{
    this.components = new System.ComponentModel.Container();
    this.timMain = new System.Windows.Forms.Timer(this.components);
    // 
    // timMain
    // 
    this.timMain.Enabled = true;
    this.timMain.Interval = 1000;
    this.timMain.Tick += new System.EventHandler(this.timMain_Tick);
    // 
    // AuctionService
    // 
    this.CanShutdown = true;
    this.ServiceName = "AuctionService";

}
Run Code Online (Sandbox Code Playgroud)

一个字:File.Create仅用于测试计时器是否打勾.由于这个原因,我有点未成熟

adr*_*nks 12

即使您正确地初始化计时器,它也没有做任何事情,因为您没有在UI中使用它.在MSDN文档指出,这必须与UI消息泵使用,该服务没有.

我建议您使用System.Threading.Timer,因为它不需要UI,更适合在服务中使用:

Timer t = new Timer(t_Tick, null, 0, 1000);
Run Code Online (Sandbox Code Playgroud)

请注意,此计时器的tick事件处理程序仅将a object作为参数.