【问题标题】:Windows service starts and exits immediatelyWindows 服务立即启动和退出
【发布时间】:2012-11-21 15:16:20
【问题描述】:

我编写了一个 Windows 服务,它又调用了一个 Web 服务。 当我从测试应用程序运行 Windows 服务时,它运行良好。但是,当我安装该服务然后启动它时,它几乎立即停止。我在日志中看到的仅有的两个条目是 ConstructorThread Started。不知道出了什么问题。

public partial class WindowsService : ServiceBase
{
    public LogManager.LogFile _log;
    public Thread m_thread;
    protected TimeSpan m_delay;

    CommonFunctions _cf = new CommonFunctions();
    DBFunctions _db = new DBFunctions();

    public WindowsService()
    {
        InitializeComponent();
        _log = new LogManager.LogFile(@"c:\test\servicelog.txt", true, true);
        _log.WriteToLog("Constructor", LogLevel.Level0);
    }


    protected override void OnStart(string[] args)
    {

       m_delay = new TimeSpan(0,0,300);
       base.OnStart(args);

        try
        {
            m_thread = new System.Threading.Thread(Execute);
            m_thread.Start();
            _log.WriteToLog("Thread Started", LogLevel.Level0);
        }
        catch (Exception ex)
        { _log.WriteToLog(ex.Message, LogLevel.Level0); }

    }

  public void Execute()
    {
        _log.WriteToLog("Begin Execute...", LogLevel.Level0);

        try
        {

            ProcessNewLMSUsers();

         }
        catch (Exception ex)
        {
             _log.WriteToLog(ex.Message.ToString());
        }

     }

    private void ProcessNewLMSUsers()
    {
        try
        {
            _log.WriteToLog("Begin: Processing new LMS Users", LogLevel.Level1);

            // Check for new users in the LMS.
            string callErrorText = "";
            bool userAdded = false;

            LMSWS.SSO lms = _cf.GetLMSSSOWS(); **// this is a web service**
            lms.Timeout = 99999;


          }

             REST OF THE CODE.................
     }

【问题讨论】:

标签: asp.net windows-services


【解决方案1】:

我看不出您的代码有什么问题。但您可以尝试在 OnStart 方法的开头添加“Thread.Sleep(20000);”代码。例如

protected override void OnStart(string[] args)
{
   Thread.Sleep(20000); 

   m_delay = new TimeSpan(0,0,300); //set a break-point here
   base.OnStart(args);

    try
    {
        m_thread = new System.Threading.Thread(Execute);
        m_thread.Start();
        _log.WriteToLog("Thread Started", LogLevel.Level0);
    }
    catch (Exception ex)
    { _log.WriteToLog(ex.Message, LogLevel.Level0); }

}

一旦您在 Windows 服务中启动此服务程序,您必须快速将源代码附加到服务程序。在 Visual Studio 中,它是菜单“调试”->“附加到进程...”。然后你可以在你的源代码的任何地方设置断点来检查出了什么问题。

【讨论】:

  • 将对System.Threading.Thread.Sleep(20000) 的调用替换为对System.Diagnostics.Debugger.Launch() 的调用。这应该会在您启动服务时弹出一个对话框,允许您打开 Visual Studio 的调试会话,并在调用时暂停代码。我被告知这可能不适用于 Windows 8,仅供参考。
  • 谢谢!并抱歉延迟回复。我能够使用 Syste.Diagnostics.Debugger.Launch() 调试代码,发现代码运行良好,但没有写入日志文件。我将值更改为 logLevel.Level0 并让它工作。但现在我有另一个问题。 Windows 服务已安装并正在运行。但是 Execute() 只执行一次。我如何让它定期执行?再次感谢!
  • 通常我在线程方法中放置一个'while'循环(这里是'Execute')。
猜你喜欢
  • 1970-01-01
  • 2017-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-01
相关资源
最近更新 更多