Kov*_*ovu 3 .net c# windows-services
我写了我的第一个Windows服务.
什么都没发生...
我尝试连接到服务,它已正确加载,但有一个断点,它永远不会打.
有任何想法吗?
代码定时器:
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
作为参数.