mirror of https://github.com/winsw/winsw
Replace thread with event
parent
d466bade0f
commit
e27a8952cb
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>net461;netcoreapp3.1</TargetFrameworks>
|
||||
<LangVersion>latest</LangVersion>
|
||||
<LangVersion>preview</LangVersion>
|
||||
<Nullable>enable</Nullable>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
</PropertyGroup>
|
||||
|
|
|
@ -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)
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue