Permalink
Browse files

Ensured that Task threads are not blocked on exit

  • Loading branch information...
1 parent 585e0e4 commit 5e5113b77916215abc6dcb367de3b1d4e4ac4805 @phatboyg committed Oct 24, 2012
Showing with 25 additions and 3 deletions.
  1. +11 −1 src/Topshelf/Hosts/ConsoleRunHost.cs
  2. +14 −2 src/Topshelf/Runtime/Windows/WindowsServiceHost.cs
@@ -15,6 +15,9 @@ namespace Topshelf.Hosts
using System;
using System.IO;
using System.Threading;
+#if !NET35
+ using System.Threading.Tasks;
+#endif
using Logging;
using Runtime;
@@ -118,9 +121,16 @@ void CatchUnhandledException(object sender, UnhandledExceptionEventArgs e)
if (e.IsTerminating)
{
_exit.Set();
+
+#if !NET35
+ // it isn't likely that a TPL thread should land here, but if it does let's no block it
+ if(Task.CurrentId.HasValue)
+ {
+ return;
+ }
+#endif
// this is evil, but perhaps a good thing to let us clean up properly.
-
int deadThreadId = Interlocked.Increment(ref _deadThread);
Thread.CurrentThread.IsBackground = true;
Thread.CurrentThread.Name = "Unhandled Exception " + deadThreadId.ToString();
@@ -16,7 +16,10 @@ namespace Topshelf.Runtime.Windows
using System.IO;
using System.Reflection;
using System.ServiceProcess;
- using System.Threading;
+ using System.Threading;
+#if !NET35
+ using System.Threading.Tasks;
+#endif
using Logging;
public class WindowsServiceHost :
@@ -205,7 +208,16 @@ void CatchUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
_log.Error("The service threw an unhandled exception", (Exception)e.ExceptionObject);
- Stop();
+ Stop();
+
+
+#if !NET35
+ // it isn't likely that a TPL thread should land here, but if it does let's no block it
+ if (Task.CurrentId.HasValue)
+ {
+ return;
+ }
+#endif
int deadThreadId = Interlocked.Increment(ref _deadThread);
Thread.CurrentThread.IsBackground = true;

0 comments on commit 5e5113b

Please sign in to comment.