Windows Service unexpectedly terminates
Posted on 2009-05-13
I have written a Windows XP Windows Service with Visual Studio 2005 in C# that terminates unexpectedly from an access violation after about 10-20 minutes. When I run the same exact code as a Windows application it can run for hours (I terminated it after 17 hours) without a problem. The service has account type LocalSystem, Manual startup type, uses TCP/IP listener and client sockets in asynchronous manner. It also uses ADO and ADOX to update and write data to an Access database.
The first thing I did was search EE and the net for solutions, but didn't find any that seemed to apply to my problem.
I put log file writing and try catch blocks everywhere in an attempt to narrow down the culprit. The problem seemed to be occuring in-between timer Elapsed events, so I got rid of the timer and used a thread with a loop that calls Thread.Sleep(15000). The Thread.Sleep() call is inside of a try/catch block. The Elapsed event handler is where the service does all of its processing. It didn't help. I bracketed the sleep call with some log file writing and confirmed that the access violation was occurring during the call to Thread.Sleep().
TheWindows Event Viewer had this info after termination:
Source: ".NET Runtime 2.0 Error Reporting"
Event ID: "1000"
Description: "Faulting application myservice.exe, version 22.214.171.124, stamp 497e0848, faulting module unknown, version 0.0.0.0, stamp 00000000, debug? 0, fault address 0x4de76542."
After this I ran "sc query myservice" from a command prompt and got:
TYPE: 110 WIN32_OWN_PROCESS (interactive)
STATE: 1 STOPPED (NOT_STOPPABLE, NOT_PAUSABLE,IGNORES_SHUTDOWN)
WIN32_EXIT_CODE: 1067 (0x42B)
SERVICE_EXIT_CODE: 0 (0x0)
I then ran "net helpmsg 1067" from a command prompt and got:
"The process terminated unexpectedly."
I have downloaded the latest updates from Microsoft for the .NET Framework, but the problem still occurs. I attached the service's process to Visual Studio for debugging, but when the AV occurs, it doesn't give any faulting module information, it just asks if I want the application to handle the exception, which it apparently can't.
I attached the service process to OllyDbg for debugging, but I am not very familiar with that app yet and didn't know what I should be looking for/at. When the AV occurred, it also wanted me to pass the unhandled exception back to the service app.
I really want this code to run as a Service and not a Windows app. Any suggestions?