144 lines
4.6 KiB
C#
144 lines
4.6 KiB
C#
using BeWoLauncher.Utils;
|
|
using BS.SharedLauncher.Information;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.IO.Pipes;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
using System.Windows.Threading;
|
|
|
|
namespace BeWoLauncher.Logic.Controller
|
|
{
|
|
public static class ProcessController
|
|
{
|
|
private static Process PlanerProcess { get; set; }
|
|
|
|
public static Application Current => Application.Current;
|
|
public static LauncherApp CurrentLauncher => Current as LauncherApp;
|
|
|
|
private static bool PlanerRunning
|
|
{
|
|
get
|
|
{
|
|
if (PlanerProcess is null)
|
|
return false;
|
|
PlanerProcess.Refresh();
|
|
return !PlanerProcess.HasExited;
|
|
}
|
|
}
|
|
|
|
internal static void CheckForOtherInstance()
|
|
{
|
|
string procName = Process.GetCurrentProcess().ProcessName;
|
|
|
|
Process[] processes = Process.GetProcessesByName(procName);
|
|
|
|
if (processes.Length > 1)
|
|
{
|
|
MessageBox.Show("BeWo Prozess läuft bereits");
|
|
StartLauncherShutdown();
|
|
}
|
|
|
|
Process[] processes2 = Process.GetProcessesByName("BeWoPlaner");
|
|
|
|
if (processes2.Length > 0)
|
|
{
|
|
MessageBox.Show("BeWo Prozess läuft bereits");
|
|
StartLauncherShutdown();
|
|
}
|
|
}
|
|
|
|
internal static void StartLauncherShutdown()
|
|
{
|
|
Current.Shutdown();
|
|
}
|
|
|
|
internal static void StartPlanerProcess()
|
|
{
|
|
// based on https://docs.microsoft.com/en-us/dotnet/standard/io/how-to-use-anonymous-pipes-for-local-interprocess-communication?redirectedfrom=MSDN
|
|
PlanerProcess = new Process();
|
|
|
|
PlanerProcess.StartInfo.FileName = BeWoPaths.GetPlanerExePath();
|
|
PlanerProcess.EnableRaisingEvents = true;
|
|
PlanerProcess.Exited += PlanerProcess_Exit;
|
|
|
|
using (AnonymousPipeServerStream pipeServer = new AnonymousPipeServerStream(PipeDirection.Out, HandleInheritability.Inheritable))
|
|
{
|
|
PlanerProcess.StartInfo.Arguments = pipeServer.GetClientHandleAsString();
|
|
PlanerProcess.StartInfo.UseShellExecute = false;
|
|
PlanerProcess.StartInfo.CreateNoWindow = false;
|
|
PlanerProcess.StartInfo.RedirectStandardOutput = true;
|
|
PlanerProcess.OutputDataReceived += MessageFromPlanerClient;
|
|
|
|
PlanerProcess.Start();
|
|
|
|
PlanerProcess.BeginOutputReadLine();
|
|
|
|
pipeServer.DisposeLocalCopyOfClientHandle();
|
|
|
|
try
|
|
{
|
|
using (StreamWriter sw = new StreamWriter(pipeServer))
|
|
{
|
|
sw.AutoFlush = true;
|
|
|
|
sw.WriteLine("C-137");
|
|
pipeServer.WaitForPipeDrain();
|
|
|
|
sw.WriteLine(ConnectionInformation.Connection.ToString());
|
|
pipeServer.WaitForPipeDrain();
|
|
}
|
|
}
|
|
catch (IOException ex)
|
|
{
|
|
|
|
}
|
|
}
|
|
}
|
|
internal static void KillPlanerProcess()
|
|
{
|
|
foreach (var process in Process.GetProcesses())
|
|
{
|
|
if (process.ProcessName.Contains("BeWoPlaner"))
|
|
{
|
|
process.Kill();
|
|
}
|
|
}
|
|
}
|
|
|
|
internal static void PlanerProcess_Exit(object sender, EventArgs e)
|
|
{
|
|
// ExitCode Übersicht:
|
|
// 100 - Nutzer hat Logout in Core gedrückt
|
|
// 0 - Nutzer hat Fenster geschlossen
|
|
// 1 - Proess über TaskManager gekillt
|
|
|
|
if (PlanerProcess.ExitCode == 100)
|
|
{
|
|
CurrentLauncher.Dispatcher.BeginInvoke(DispatcherPriority.Render, (Action)delegate {
|
|
Thread.Sleep(500);
|
|
ViewController.RestartView();
|
|
});
|
|
return;
|
|
}
|
|
|
|
CurrentLauncher.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)StartLauncherShutdown);
|
|
}
|
|
|
|
private static void MessageFromPlanerClient(object sender, DataReceivedEventArgs args)
|
|
{
|
|
var data = args.Data;
|
|
|
|
if (data == "GUI geladen")
|
|
{
|
|
ViewController.LauncherWindow.Dispatcher.BeginInvoke(DispatcherPriority.Render, (Action)(() => ViewController.LauncherWindow.Hide()));
|
|
}
|
|
}
|
|
}
|
|
}
|