【发布时间】:2013-10-17 22:26:38
【问题描述】:
我正在尝试将控制台应用程序转换为 Windows 服务。我试图让服务的 onstart 方法在我的类中调用一个方法,但我似乎无法让它工作。我不确定我这样做是否正确。我在服务中的哪个位置放置课程信息
protected override void OnStart(string[] args)
{
EventLog.WriteEntry("my service started");
Debugger.Launch();
Program pgrm = new Program();
pgrm.Run();
}
来自评论:
namespace MyService {
static class serviceProgram {
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main() {
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[] {
new Service1()
};
ServiceBase.Run(ServicesToRun);
}
}
}
【问题讨论】:
-
您是否将项目类型从控制台应用程序更改为 Windows 应用程序?你打电话给
ServiceBase.Run? -
是的,我在我的解决方案中创建了一个新项目作为 Windows 服务。
-
namespace MyService { static class serviceProgram { ///
/// 应用程序的主要入口点。 /// static void Main() { ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] { new Service1() }; ServiceBase.Run(ServicesToRun); } } }
标签: c# service console onstart