首先Windows服务 - 计时器似乎不勾选 [英] First Windows Service - timer seems not to tick
问题描述
我写我的第一个Windows服务。
- 创建WS项目
- 重命名服务
- 将一个计时器到中间
- 启用它,勾选为1秒
- 创建蜱一个logfie当不存在
- 安装服务
- 运行服务
什么也没有发生......
我尝试连接到服务,它的正确加载,但在这一个断点,它从来没有击中。
任何想法?
code定时:
私人无效timMain_Tick(对象发件人,EventArgs的)
{
如果(File.Exists(C:/test.txt!))
File.Create(C:/test.txt);
}
code初始化:
私人无效的InitializeComponent()
{
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.
- Create WS project
- Rename Service
- Drag a timer into the middle
- Enable it, tick to 1s
- Create a logfie in tick when not exists
- Install the service
- 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屋!