Files
BeWoPlaner/BeWoLauncher/LauncherApp.xaml.cs
rene 25438ebd2d Zwischenstand:
- Download Req und Res eingeführt
- Version XML + version.config
2022-03-09 16:29:00 +01:00

205 lines
6.7 KiB
C#

using BeWoLauncher.Components;
using BeWoLauncher.ServiceProxy;
using BeWoLauncher.WindowImp;
using BS.SharedLauncher;
using BS.SharedLauncher.Information;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Reflection;
using System.Security.Policy;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Threading;
using System.Xml;
namespace BeWoLauncher
{
/// <summary>
/// Interaktionslogik für "App.xaml"
/// </summary>
public partial class LauncherApp : Application
{
public static string ShortVersion => "3";
public static string Version => "Version 3.0";
public static LauncherWindow Window { get; set; }
public static LauncherApp CurrentLauncher => Current as LauncherApp;
private static Process PlanerProcess { get; set; }
private static bool PlanerRunning
{
get
{
if (PlanerProcess is null)
return false;
PlanerProcess.Refresh();
return !PlanerProcess.HasExited;
}
}
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
CheckForOtherInstance();
// Update Server Adresses
ConnectionInformation.LauncherServerAddress = ConnectionInformation.LauncherServerAddressOrigin.ToString();
LauncherServiceFacade.UpdateServerAddresses(ConnectionInformation.LauncherServerAddress);
Dispatcher.UnhandledException += (s, ex)
=>
{
if (ex.Exception == null || String.IsNullOrEmpty(ex.Exception.Message) ||
(!ex.Exception.ToString().Contains("DevExpress.Xpf.Bars.Customization.CustomizationControl.OnLoaded")
&& !ex.Exception.ToString().Contains("DevExpress.Xpf.Grid.Native.SelectionStrategyCellBase.IsCellSelected")))
{
ShowError("Unbehandelter Fehler", ex.Exception, true);
}
ex.Handled = true;
};
FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement), new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));
//XmlDocument doc = new XmlDocument();
//doc.Load(Path.Combine("d:/ServerBeWoDataExample", "version.config"));
//var default2 = doc.SelectSingleNode("/config/default/version");
//var defaultValue = default2.InnerXml;
//var specific2 = doc.SelectSingleNode("/config/tenant[@id='demo3']/version");
//var specificVersion = specific2.InnerXml;
}
private void CheckForOtherInstance()
{
string procName = Process.GetCurrentProcess().ProcessName;
Process[] processes = Process.GetProcessesByName(procName);
if (processes.Length > 1)
{
MessageBox.Show("BeWo Prozess läuft bereits");
StartShutdown();
}
}
#region ShowError
public static void ShowError(Exception ex) => ShowError(null, ex, true);
public static void ShowError(string message, Exception ex, bool showDetails)
{
ShowError(message, ex.ToString(), showDetails);
}
public static void ShowError(string message, string ex, bool showDetails)
{
if (Window is null)
{
MessageBox.Show(ex, message);
}
else
{
Window.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() =>
Window.ShowControlAsModalPopUp(new ExceptionMessageBox(message, ex, showDetails))));
}
}
#endregion
public static void StartBeWo()
{
// 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 = UpdatePathManager.GetPlanerPath();
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.Start();
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)
{
}
}
Window.Hide();
}
public static void StartShutdown()
{
Current.Shutdown();
}
private 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.Normal, (Action)delegate{
Window.ShowLoginPage();
Window.Show();
});
return;
}
CurrentLauncher.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)StartShutdown);
}
private void Application_Exit(object sender, ExitEventArgs e)
{
BeWo_Process_Kill();
}
private static void BeWo_Process_Kill()
{
foreach (var process in Process.GetProcesses())
{
if (process.ProcessName.Contains("BeWoPlaner"))
{
process.Kill();
}
}
}
}
}