我正在从msdn链接学习窗口服务: http://msdn.microsoft.com/en-us/library/zt39148a(v=vs.110).aspx
我已经正确创建,安装第一次....当我尝试从Service.msc启动时..它正在抛出错误:
错误1053服务未响应启动或控制请求
这是我的代码:
public partial class ASMSService : ServiceBase
{
private Timer myTimer;
TimeSpan setTime;
private DateTime previousDate;
private DateTime todaysDate;
public ASMSService()
{
InitializeComponent();
if (!System.Diagnostics.EventLog.SourceExists("MySource"))
{
System.Diagnostics.EventLog.CreateEventSource(
"MySource", "MyNewLog");
}
eventLog1.Source = "MySource";
eventLog1.Log = "MyNewLog";
}
protected override void OnStart(string[] args)
{
myTimer = new System.Threading.Timer(new TimerCallback(TimerAction1));
SetTimer(11, 07, 00);
}
protected override void OnStop()
{
}
private void SetTimer(int hours, int minutes, int seconds)
{
todaysDate = DateTime.Today;
previousDate = todaysDate.AddDays(-1);
setTime = todaysDate.AddHours(hours).AddMinutes(minutes).AddSeconds(seconds).TimeOfDay; ;
}
private void TimerAction1(object e)
{
//some Code
}
}
这是设计代码
partial class ASMSService
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.eventLog1 = new System.Diagnostics.EventLog();
((System.ComponentModel.ISupportInitialize)(this.eventLog1)).BeginInit();
//
// ASMSService
//
this.ServiceName = "ASMSService";
((System.ComponentModel.ISupportInitialize)(this.eventLog1)).EndInit();
}
#endregion
private System.Diagnostics.EventLog eventLog1;
}
这是程序类:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new ASMSService()
};
ServiceBase.Run(ServicesToRun);
}
}
我已经阅读了类似的帖子..有些帖子建议安装microsoft Patch ..其他建议创建的对象应该正确处理..我也尝试在Onstop方法中做到这一点..但它不工作..一些帖子建议应该在Main()方法中调用ServiceBase.Run()方法...它也出现在我的代码中
请建议
答案 0 :(得分:1)
我可以看到ASMSService的计时器存在一个很大的问题:
创建:
myTimer = new System.Threading.Timer(new TimerCallback(TimerAction1));
但它绝不是started:
private void SetTimer(int hours, int minutes, int seconds)
{
todaysDate = DateTime.Today;
previousDate = todaysDate.AddDays(-1);
setTime = todaysDate.AddHours(hours).AddMinutes(minutes).AddSeconds(seconds).TimeOfDay; ;
// You have set the setTime field, otherwise the timer will still have the infinite dueTime and interval - it is not running at all
// You should call SetChange method to start it.
this.mytimer.SetChange(0, (Int64)setTime.ToMilliseconds());
}
如果您使用SetChange ,则需要the simplest constructor方法来启动计时器
您可能还想阅读以下链接,他们正在处理类似情况并提供一些需要考虑的见解:
Windows service with timer
System.Threading.Timer Not Starting?