简体   繁体   English

C#将控制台应用程序转换为服务

[英]C# Converting Console App to Service

I am trying to convert a console app to a Windows service. 我正在尝试将控制台应用程序转换为Windows服务。 I am trying to have the onstart method of the service call a method in my class but I can;t seem to get it to work. 我试图让服务的onstart方法在我的类中调用一个方法,但我可以;似乎让它工作。 I'm not sure I am doing this correctly. 我不确定我是否正确这样做。 Where do I put the class information in the service 我在哪里将课程信息放入服务中

protected override void OnStart(string[] args)
{
   EventLog.WriteEntry("my service started");
   Debugger.Launch();
   Program pgrm = new Program();
   pgrm.Run();
}

From the comment: 来自评论:

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);
  }
 }
}

The MSDN documentation on Windows Services is really good and has everything you need to get started. Windows服务上的MSDN文档非常好,并且具备入门所需的一切。

The problem you're having is because of your OnStart implementation, that's only supposed to be used to set up the service so it's ready to start, the method must return promptly. 您遇到的问题是因为您的OnStart实现,它只应该用于设置服务以便它可以启动,该方法必须立即返回。 Usually you'd run the bulk of the code on another thread or in a timer. 通常,您可以在另一个线程或计时器上运行大量代码。 See the page for OnStart for confirmation. 请参阅OnStart页面以进行确认。

Edit: Without knowing what your windows service will do, it's hard to tell you how to implement it but let's say you wanted to run a method every 10 seconds while the service is running: 编辑:不知道你的Windows服务会做什么,很难告诉你如何实现它,但是假设你想在服务运行时每隔10秒运行一次方法:

public partial class Service1 : ServiceBase
{
    private System.Timers.Timer _timer; 

    public Service1()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
#if DEBUG
        System.Diagnostics.Debugger.Launch(); // This will automatically prompt to attach the debugger if you are in Debug configuration
#endif

        _timer = new System.Timers.Timer(10 * 1000); //10 seconds
        _timer.Elapsed += TimerOnElapsed;
        _timer.Start();
    }

    private void TimerOnElapsed(object sender, ElapsedEventArgs elapsedEventArgs)
    {
        // Call to run off to a database or do some processing
    }

    protected override void OnStop()
    {
        _timer.Stop();
        _timer.Elapsed -= TimerOnElapsed;
    }
}

Here, the OnStart method returns immediately after setting up the timer and TimerOnElapsed will be run on a worker thread. 这里, OnStart方法在设置定时器后立即返回, TimerOnElapsed将在工作线程上运行。 I also added a call to System.Diagnostics.Debugger.Launch(); 我还添加了对System.Diagnostics.Debugger.Launch();的调用System.Diagnostics.Debugger.Launch(); which will make debugging alot easier. 这将使调试更容易。

If you have some other requirements, please edit your question or post a comment. 如果您有其他要求,请编辑您的问题或发表评论。

Do yourself the biggest favor and use topshelf http://topshelf-project.com/ to create your service. 做自己最大的忙,并使用topshelf http://topshelf-project.com/来创建您的服务。 There is nothing easier that I have seen. 我见过没有比这更容易了。 Their documentation is supperb and deployment could not be simplier. 他们的文档是supperb,部署不能简化。 c:/path to service/service.exe install. c:/ service / service.exe安装路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM