Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple threading issue with windows service

I havent written a windows service before and thought everything was going well until I deployed it to live. In dev it works fine and the polling it great, but as soon as it goes into production it falls on its backside after its first loop.

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()

and here is the code that is doing the looping:

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

        }

Does anyone have any idea why this would fall over after it runs the first time? am I doing something stupid?

This is the method causing the issue:

public static string GetXmlFromFeed(string strUrl) { var rssReq = WebRequest.Create(strUrl); var rep = rssReq.GetResponse(); return new StreamReader(rep.GetResponseStream()).ReadToEnd(); }

On the GetResponse()

possible a time out and nothing to do with the threading at all then

like image 896
JamesStuddart Avatar asked May 28 '26 15:05

JamesStuddart


1 Answers

Looking at the exception stack trace it seems that the StartChecks throws an exception which is not handled and it propagates to the calling thread (this behavior was introduced in .NET 2.0 as before exceptions thrown in child threads weren't propagated).

Try putting a try/catch around it in order to handle this exception.

like image 114
Darin Dimitrov Avatar answered May 31 '26 06:05

Darin Dimitrov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!