Replace thread with event

pull/423/head
NextTurn 2020-02-25 00:00:00 +08:00 committed by Next Turn
parent d466bade0f
commit e27a8952cb
3 changed files with 14 additions and 26 deletions

View File

@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Threading;
using log4net;
using static WinSW.Native.ProcessApis;
@ -194,32 +193,20 @@ namespace WinSW.Util
// monitor the completion of the process
if (callback != null)
{
StartThread(() =>
{
processToStart.WaitForExit();
callback(processToStart);
});
}
}
/// <summary>
/// Starts a thread that protects the execution with a try/catch block.
/// It appears that in .NET, unhandled exception in any thread causes the app to terminate
/// http://msdn.microsoft.com/en-us/library/ms228965.aspx
/// </summary>
public static void StartThread(ThreadStart main)
{
new Thread(() =>
processToStart.Exited += (_, _) =>
{
try
{
main();
callback(processToStart);
}
catch (Exception e)
{
Logger.Error("Thread failed unexpectedly", e);
}
}).Start();
};
processToStart.EnableRaisingEvents = true;
}
}
}

View File

@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>net461;netcoreapp3.1</TargetFrameworks>
<LangVersion>latest</LangVersion>
<LangVersion>preview</LangVersion>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

View File

@ -297,6 +297,7 @@ namespace WinSW
this.LogEvent("Stopping " + this.descriptor.Id);
Log.Info("Stopping " + this.descriptor.Id);
this.orderlyShutdown = true;
this.process.EnableRaisingEvents = false;
if (stopArguments is null)
{