[英]Simple threading issue with windows service
我以前没有写过Windows服务,并且认为一切进展顺利,直到我部署它为止。 在开发中它工作得很好并且轮询它很棒,但是一旦它投入生产它在它的第一个循环后落在它的背面。
The exception I recieve is:
Application: ProgramName.WinService.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.Exception
Stack:
at ProgramName.WinService.UpdateChecker.StartChecks()
at ProgramName.WinService.UpdateChecker.StartPolling()
at System.Threading.ThreadHelper.ThreadStart_Context(System.Object)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
at System.Threading.ThreadHelper.ThreadStart()
这是执行循环的代码:
private readonly Thread pollingThread;
public UpdateChecker()
{
pollingThread = new Thread(StartPolling);
InitializeComponent();
}
protected override void OnStart(string[] args)
{
pollingThread.Start();
}
protected override void OnStop()
{
pollingThread.Abort();
}
protected void StartPolling()
{
do
{
StartChecks();
//10 seconds
Thread.Sleep(10000);
} while (true);
}
有没有人知道为什么在第一次运行后会出现这种情况? 我做了些蠢事吗?
这是导致问题的方法:
public static string GetXmlFromFeed(string strUrl){var rssReq = WebRequest.Create(strUrl); var rep = rssReq.GetResponse(); 返回new StreamReader(rep.GetResponseStream())。ReadToEnd(); }
在GetResponse()上
可能是超时而与线程无关
查看异常堆栈跟踪似乎StartChecks
抛出一个StartChecks
的异常并将其传播到调用线程( 此行为是在.NET 2.0中引入的,因为在子线程中抛出的异常未被传播之前)。
尝试在其周围放置一个try/catch
以处理此异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.