The MyService service terminated unexpectedly. It has done

I have a C# windows service that I would like to update silently. It all works great except that when it installs an update I get this error in the System event log:

The MyService service terminated unexpectedly. It has done this 8 time(s).

It happens when I call auBackend.InstallNow();which I beleive from documentation calls Environment.Exit(0)

Before calling auBackend.InstallNow() I abort my worker thread, join it, all seems good here. That is all the "spin down" I do, I don't stop the service, such as this.Stop(). I don't know if I need to do more?

I am looking to eliminate "The MyService service terminated unexpectedly. It has done this 8 time(s)."

If i update "manually" by runninng wyupdate.exe it all works without any errors.

Hope you can help or feed be a hint.

Thanks,Jakob

Hey Jakob,

What happens when you call Environment.Exit(0) in your service? Do you get the same "terminated unexpectedly" error?

Yes, if I just call Environment.Exit(0) instead of auBackend.InstallNow() it gives me the same error.I don't call any Dispose()'s or other stuff, I just abort worker threads. Perhaps my spindown needs to do more.Thanks,Jakob

protected override void OnStart(string[] args_notused) {

...

auBackend.ReadyToBeInstalled += auBackend_ReadyToBeInstalled; auBackend.UpdateSuccessful += auBackend_UpdateSuccessful;

//TODO: use the failed events for logging & error reporting: // CheckingFailed, DownloadingFailed, ExtractingFailed, UpdateFailed

// Initialize() and AppLoaded() must be called after events have been set. // Note: If there's a pending update to be installed, wyUpdate will be // started, then it will talk back and say "ready to install, // you can close now" at which point your app will be closed. auBackend.Initialize(); auBackend.AppLoaded();

... }

void auBackend_ReadyToBeInstalled(object sender, EventArgs e) { if (auBackend.UpdateStepOn == UpdateStepOn.UpdateReadyToInstall) { SpinDown(); // Aborts worker thread(s), joins/waits for threads to stop

System.Threading.Thread.Sleep(3000); // Wait for 3 seconds for no particular reason

// Inserted to see if "service terminated unexpectedly" still occurs. // Error still occurs right after "Calling Environment.Exit(0)" is logged. Logger.Log("Calling Environment.Exit(0)", 1088); Environment.Exit(0);

Logger.Log("Calling InstallNow()", 1088); auBackend.InstallNow();

} }

protected void SpinDown() { Logger.Log("SpinDown started on " + _threadList.Count.ToString() + " threads.", 1012);

foreach (Thread thread in _threadList) { thread.Abort(); thread.Join(new TimeSpan(0, 0, 30)); // Give each thread 30 seconds to end }

_threadList.Clear();

Logger.Log("SpinDown completed", 1012); }

It's very likely that Thread.Abort is causing all the problems. See: How To Stop a Thread in .NET (and Why Thread.Abort is Evil).

I revamped my workers to instead just check a volatile bool avoiding Aborts. The workers (still) close down perfectly fine using that method. But I still get the error on Environment.Exit(0).

The SpinDown now looks like this.

protected void SpinDown() { Logger.Log("SpinDown started on " + _threadList.Count.ToString() + " threads.", 1012);

// Signal tasks to end _submitEventsTaskParameters.Running = false; _checkForUpdatesTaskParameters.Running = false;

foreach (Thread thread in _threadList) { // thread.Abort(); // Replaced by less evil signals to end tasks (above) thread.Join(new TimeSpan(0, 0, 30)); // Give each thread 30 seconds to end }

_threadList.Clear();

Logger.Log("SpinDown completed", 1012); }

I will try different things in my code. I don't think the problem is a wyupdate problem. I was hoping I did something wrong with wyupdate - you know with a known easy "fix/info for newbies", but no such luck I think.

I'll update here if/when I come up with anything.

Thanks,Jakob

Can't figure out how to avoid the error in the System log, no matter how "gracefully" I spin down before calling auBackend.InstallNow() - or Environment.Exit(0) for that matter.

I think OS - one is Windows 2008 R2 the other Windows 7 - detects that the Service stops without having been asked to stop for example though services.mcs. And then OS logs the The service terminated unexpectedly. It has done this x time(s).

Stopping, starting, restarting etc. always works fine without any errors; if I use service.msc or NET START MyService, NET STOP MyService etc.

I am now wondering if should replace the call auBackend.InstallNow() with shelling a process running wyupdate.exe "manually" instead. That way (perhaps) the stop and start of the service comes from outside instead of within.

I'm going to look into this. Environment.Exit(0) shouldn't cause a "terminated unexpectedly" error. You're not running the AutomaticUpdaterBackend on a separate thread are you? Because that can be the cause of the problem.

Also, have you tried attaching a debugger to the service to see where the exception/crash is occurring?

I am now wondering if should replace the call auBackend.InstallNow() with shelling a process running wyupdate.exe "manually" instead. That way (perhaps) the stop and start of the service comes from outside instead of within.

Well, you can always use wyUpdate in standalone mode to automatically update your Window Service. See: How to Silently update a Windows Service

I gave up on using AutomaticUpdaterBackend for now.

Instead I switched to use wyUpdate in standalone mode calling "wyupdate.exe -quickcheck -justcheck -noerr" followed by "wyupdate.exe /fromservice" if exitcode from quickcheck suggests an update is available. I can do that on a worker thread that checks periodically. It works perfectly, stops the service, updates and restarts without any errors.

Thanks for your help, and I'll buy wyupdate - it is a great tool for seamless automatic updates!