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.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Diagnostics; using System.Diagnostics;
using System.Threading;
using log4net; using log4net;
using static WinSW.Native.ProcessApis; using static WinSW.Native.ProcessApis;
@ -194,32 +193,20 @@ namespace WinSW.Util
// monitor the completion of the process // monitor the completion of the process
if (callback != null) if (callback != null)
{ {
StartThread(() => processToStart.Exited += (_, _) =>
{ {
processToStart.WaitForExit(); try
callback(processToStart); {
}); callback(processToStart);
} }
} catch (Exception e)
{
Logger.Error("Thread failed unexpectedly", e);
}
};
/// <summary> processToStart.EnableRaisingEvents = true;
/// 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(() =>
{
try
{
main();
}
catch (Exception e)
{
Logger.Error("Thread failed unexpectedly", e);
}
}).Start();
} }
} }

View File

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

View File

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