Change signalhandling netcore

This commit is contained in:
Philip Schell 2019-11-29 14:44:45 +01:00
parent ec93004c2b
commit 9326ab8659
2 changed files with 9 additions and 12 deletions

View File

@ -4,27 +4,26 @@ using System.Threading;
namespace BlubbFish.Utils.IoT.Bots {
public abstract class ABot {
#if !NETCOREAPP
private Thread sig_thread;
#endif
private Boolean RunningProcess = true;
protected ProgramLogger logger = new ProgramLogger();
#if !NETCOREAPP
private void SetupShutdown(Object sender, ConsoleCancelEventArgs e) {
e.Cancel = true;
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.SetupShutdown: Signalhandler Windows INT recieved.");
this.RunningProcess = false;
}
#else
private void Default_Unloading(AssemblyLoadContext obj) {
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.SetupShutdown: Signalhandler Windows INT recieved.");
this.RunningProcess = false;
}
#endif
protected void WaitForShutdown() {
if(Type.GetType("Mono.Runtime") != null) {
#if !NETCOREAPP
#if !NETCOREAPP
this.sig_thread = new Thread(delegate () {
Mono.Unix.UnixSignal[] signals = new Mono.Unix.UnixSignal[] {
new Mono.Unix.UnixSignal(Mono.Unix.Native.Signum.SIGTERM),
@ -40,13 +39,11 @@ namespace BlubbFish.Utils.IoT.Bots {
});
this.sig_thread.Start();
#endif
} else {
#if NETCOREAPP
} else {
AssemblyLoadContext.Default.Unloading += this.Default_Unloading;
#else
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.WaitForShutdown: Signalhandler Netcore attached.");
Console.CancelKeyPress += new ConsoleCancelEventHandler(this.SetupShutdown);
Console.WriteLine("BlubbFish.Utils.IoT.Bots.Bot.WaitForShutdown: Signalhandler Windows attached.");
#endif
}
while(this.RunningProcess) {
Thread.Sleep(100);
@ -56,9 +53,11 @@ namespace BlubbFish.Utils.IoT.Bots {
public virtual void Dispose() {
#if !NETCOREAPP
if(this.sig_thread != null && this.sig_thread.IsAlive) {
this.sig_thread.Abort();
}
#endif
}
}
}

View File

@ -1,5 +1,4 @@
#if NETCOREAPP
#else
#if !NETCOREAPP
using System.Reflection;
using System.Resources;
using System.Runtime.InteropServices;
@ -37,7 +36,7 @@ using System.Runtime.InteropServices;
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.2.1")]
[assembly: AssemblyFileVersion("1.2.1")]
#endif
/*
* 1.1.0 Remove Helper from Bot-Utils
* 1.1.1 Update to local librarys
@ -53,4 +52,3 @@ using System.Runtime.InteropServices;
* 1.2.0 Refactor Bot to ABot and refere MultiSourceBot, Webserver and Bot to it. Add MultiSourceBot. Rewrite Mqtt module so that it not need to watch the connection.
* 1.2.1 When using Dispose, kill also mqtt connection and other tiny fixes
*/
#endif