Windows服务立即启动和退出

时间:2012-11-21 15:16:21

标签: asp.net windows-services

我编写了一个Windows服务,后者又调用了一个Web服务。 当我从测试应用运行Windows服务时,它运行良好。但是,当我安装该服务然后启动它时,它几乎立即停止。我在日志中看到的唯一两个条目是构造函数线程已启动。不确定是什么问题。

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.................
     }

1 个答案:

答案 0 :(得分:0)

我看不出你的代码有什么问题。但你可以尝试在OnStart方法的开头放一个“Thread.Sleep(20000);”代码。 e.g。

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中,它的菜单是“Debug” - > “附加到流程......”。然后你可以在任何地方的源代码中设置断点来检查出错的地方。

相关问题