首先Windows服务 - 计​​时器似乎不勾选 [英] First Windows Service - timer seems not to tick

查看:114
本文介绍了首先Windows服务 - 计​​时器似乎不勾选的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写我的第一个Windows服务。

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

什么也没有发生......

我尝试连接到服务,它的正确加载,但在这一个断点,它从来没有击中。

任何想法?


code定时:

 私人无效timMain_Tick(对象发件人,EventArgs的)
    {
        如果(File.Exists(C:/test.txt!))
        File.Create(C:/test.txt);
    }
 

code初始化:

 私人无效的Ini​​tializeComponent()
    {
        this.components =新System.ComponentModel.Container();
        this.timMain =新System.Windows.Forms.Timer(this.components);
        //
        // timMain
        //
        this.timMain.Enabled = TRUE;
        this.timMain.Interval = 1000;
        this.timMain.Tick + =新System.EventHandler(this.timMain_Tick);
        //
        // AuctionService
        //
        this.CanShutdown = TRUE;
        this.ServiceName =AuctionService;

    }
 

一个字:该File.Create只有当计时器滴答测试。我是因为这一点uncreatve =)

解决方案

即使你是正确初始化计时器,它不会因为你不使用它的用户界面做任何事情。该 MSDN文档的状态,它的必须与使用UI消息泵,其中一个服务没有。

我推荐你使用<一个href="http://msdn.microsoft.com/en-us/library/system.threading.timer.aspx">System.Threading.Timer代替,因为它不要求一个用户界面和更适合在服务使用:

  TIMER T =新的定时器(t_Tick,NULL,0,1000);
 

请注意,该Tick事件处理程序,此计时器只需要一个对象作为参数。<​​/ P>

I wrote my first windows service.

  1. Create WS project
  2. Rename Service
  3. Drag a timer into the middle
  4. Enable it, tick to 1s
  5. Create a logfie in tick when not exists
  6. Install the service
  7. Run the Service

Nothing happens...

I try to attach to the service, it's loaded correctly, but with a breakpoint in it, it never hits.

Any ideas?


Code Timer:

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

Code initialize:

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";

    }

One word: The File.Create is only to test if the timer tick. I was a little uncreatve because of that =)

解决方案

Even though you are initialising the timer correctly, it is not doing anything because you are not using it in a UI. The MSDN docs state that it must be used with a UI message pump, which a service does not have.

I recommend you use a System.Threading.Timer instead as it does not require a UI and is more appropriate for use in a service:

Timer t = new Timer(t_Tick, null, 0, 1000);

Note that the tick event handler for this timer only takes an object as an argument.

这篇关于首先Windows服务 - 计​​时器似乎不勾选的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆