views:

100

answers:

4

Hi, My windows service is using a Thread (not a timer) which is always looping and sleeps for 1 second every loop using : evet.WaitOne(interval);

When I start the service it works fine and I can see in the task manager that it is running, consuming and releasing memory, consuming processor ... etc that is all normal, but after a while (random amount of time) the service simply stops!! it is still there in the task manager but it is not consuming any processor work now and its consumption to the memory is not changing. it simply (died but still there in the task manager like a Zombie).

I know that many exceptions might have happened during running the service (it is really doing many things) but all those exceptions are handled in Try catch blocks, so why is my "always looping" thread stops ??? This thread also logs every time he loops, when he is freezig in this way he is not logging anything (of course)

A: 

Have you tried using Thread.Sleep(interval) instead of the wait handle?

Codebrain
It's not good practice to use Thread.Sleep since it involves more work underneath. Waiting on the event - like the original poster does - is better.
Yann Schwartz
Nope, but I think that using evet.WaitOne(interval);gives the service the ability to react to any outer requests (like the request to stop the service)
TB
A: 

If you're using .Net 2.0 or later, an unhandled exception in the background thread should kill the process entirely. So it's not likely to be the case.

To make sure what's happening in your service, I'd suggest to use windbg, along the lines of what I've described a while ago on another question http://stackoverflow.com/questions/1716988/what-can-cause-asp-net-to-stop-responding/1717199#1717199

I'd suggest pinpointing the worker thread and issue a !CLRStack on it to see what's holding it. If the thread is nowhere to be found, it may have crashed (but then your hosting process should have crashed too) or it may have exited for one reason or another. In that case, use logging more generously.

Yann Schwartz
I will try, but I wonder how the "alwayes running" thread can stop !! I am using:while(true){}so it should loop forever, and if it crashed then (like you said) the entire service should crash.
TB
Then it's likely stuck waiting on something (IO, another sync object, whatever). Try to attach with the debugger, or fire windbg on it.
Yann Schwartz
A: 

I found out that there is an exception being thrown at some time and that exception is opening a Dialog (terminate application or cancel to debug). This dialog is strange because I am 100% sure that all my service code is safe and all exceptions are handled. but it seems that one of the 3rd party DLLs that I am calling from this service is calling an unsafe Win32 library, which is throwing that exception (I am not sure about this until now but that is most possible, because if my code is causing the exception then the service will terminate).

To discover this dialog while the Windows Service is running I made the service run under (Local system account) and I checked the box (Allow service to interact with desktop), without this I would never see that dialog even if it is shown by the called DLL.

Thanks for all those who tried to help

TB
A: 

Hi, I have kind of same issue but couldn't figure it out. any idea would be appreciated?

I start a thread in windows service like

workerThread = new Thread(new ThreadStart(ServiceWorkerMethod)); workerThread.Start();

and follwoing messages written out:

workerThread.IsAlive --IS--> True workerThread.IsBackground --IS--> False IsThreadPoolThread --IS--> False workerThread.Priority --IS--> Normal workerThread.ThreadState --IS--> Running

but looks like ServiceWorkerMethod never called! it works fine in dev server. the only deference is processor type one intel the other AMD. not sure if it matters. Also there is no error on Event Viewer log and all errors handled by try catch.

any idea?

Developer