我写我的第一个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消息泵,其中一个服务没有。
我推荐你使用System.Threading.Timer代替,因为它不要求一个用户界面和更适合在服务使用:
TIMER T =新的定时器(t_Tick,NULL,0,1000);
请注意,该Tick事件处理程序,此计时器只需要一个对象
作为参数。
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 ServiceNothing 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.
相关推荐
最新文章