Window Service应用程序出错 [英] Error In Window Service application
本文介绍了Window Service应用程序出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了一个帖子
i created a thread
RMSEmailReaderWindowServiceThread = new Thread(new ThreadStart(continuouslyThreadRunMethod));
////start the thread
RMSEmailReaderWindowServiceThread.Start();
i创建了一个方法
i created a method
public void continuouslyThreadRunMethod()
{
// we're going to wait 5 minutes between calls to CTASK, so
// set the interval to 300000 milliseconds
// (1000 milliseconds = 1 second, 5 * 60 * 1000 = 300000)
int waitTime = 300000; // 5 minutes
// this variable tracks how many milliseconds have gone by since
// the last call to CTASK. Set it to zero to indicate we're
// starting fresh
//int elapsed = 0;
// because we don't want to use 100% of the CPU, we will be
// sleeping for 1 second between checks to see if it's time to
// call CTASK
ServiceController RMSEmailReaderStatus = new ServiceController("RMSEmailReader");
try
{
// check the status of the service.if service is stop mean thread have to stop
//!(RMSEmailReaderStatus.Status.Equals("ServiceControllerStatus.Stopped"))
while (true)
{
ClientServerConnection();
Thread.Sleep(waitTime);
if (RMSEmailReaderStatus.Status.Equals("ServiceControllerStatus.Stopped"))
this.OnStop();
}//while close
}//try close
//for thread exception
catch (ThreadAbortException TAE)
{
// we want to eat the excetion because we don't care if the
// thread has aborted since we probably did it on purpose by
// stopping the service.
//if exception is raised close the application
RMSDotNetExceptionHistory RDEH_Record = new RMSDotNetExceptionHistory();
RDEH_Record.ModuleName = "EmailReader";
RDEH_Record.ExceptionCode = TAE.ToString();
RDEH_Record.ExceptionDescription = TAE.ToString();
RDEH_Record.ExceptionDateAndTime = DateTime.Now;
RDEH_Record.AdditionalComments = "Continously Thread Run Method created an exceptions";
InsertARecordIntoRMSDotNetExceptionHistory(RDEH_Record);
//Environment.Exit(0);
}//catch close
catch (Exception ex)
{
RMSDotNetExceptionHistory RDEH_Record = new RMSDotNetExceptionHistory();
RDEH_Record.ModuleName = "EmailReader";
RDEH_Record.ExceptionCode = ex.ToString();
RDEH_Record.ExceptionDescription = ex.ToString();
RDEH_Record.ExceptionDateAndTime = DateTime.Now;
RDEH_Record.AdditionalComments = "Continously Thread Run Method created an exceptions";
InsertARecordIntoRMSDotNetExceptionHistory(RDEH_Record);
}
}//Continouslytheard run method close
错误: -
无法停止服务。 System.NullReferenceException:对象引用未设置为对象的实例。
at RMSEmailReader.RMSEmailReaderClass.OnStop()
at System.ServiceProcess.ServiceBase.DeferredStop()
Error:-
Failed to stop service. System.NullReferenceException: Object reference not set to an instance of an object.
at RMSEmailReader.RMSEmailReaderClass.OnStop()
at System.ServiceProcess.ServiceBase.DeferredStop()
推荐答案
而不是使用this.OnStop()进行stooping线程,你只需使用break语句,这将打破你的while循环,你的线程就完成了。
instead of using this.OnStop() for stooping thread you just simply use break statement, which will break your while loop and your thread is finished.
if (RMSEmailReaderStatus.Status.Equals("ServiceControllerStatus.Stopped"))
break;
这篇关于Window Service应用程序出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文