SafeDispatch/Tetra_GW/Program.cs
2024-02-22 18:43:59 +02:00

99 lines
3.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Threading;
using System.IO;
namespace Tetra_GW
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static MainForm MainWindow = null;
static String filename = "";
static void Main(string[] args)
{
Console.Title = "Tetra_GWConsole";
if (args.Length > 0)
{
if (args[0] != "-c")
{
// hide the console window
setConsoleWindowVisibility(false, Console.Title);
}
if ((args.Length > 2) && (args[1] == ">"))
filename = args[2].ToString().Trim();
}
else
{
setConsoleWindowVisibility(false, Console.Title);
}
try
{
filename = filename.Trim();
if (filename.Length > 1)
{
FileStream filestream = new FileStream(filename, FileMode.Create);
var streamwriter = new StreamWriter(filestream);
streamwriter.AutoFlush = true;
Console.SetOut(streamwriter);
Console.SetError(streamwriter);
}
}
catch (Exception ex)
{
Console.WriteLine("Exeception:" + ex.ToString());
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
MainWindow = new MainForm();
Application.Run(MainWindow);
}
static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
MessageBox.Show(e.Exception.Message, "ThreadException Tetra_GW.Please restart the application.");
// here you can log the exception ...
}
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
MessageBox.Show((e.ExceptionObject as Exception).Message, "Unhandled UI Exception Tetra_GW.Please restart the application.");
// here you can log the exception ...
}
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
public static void setConsoleWindowVisibility(bool visible, string title)
{
IntPtr hWnd = IntPtr.Zero;
while (hWnd == IntPtr.Zero)
{
hWnd = FindWindow(null, title);
if (hWnd != IntPtr.Zero)
{
if (!visible)
//Hide the window
ShowWindow(hWnd, 0); // 0 = SW_HIDE
else
//Show window again
ShowWindow(hWnd, 1); //1 = SW_SHOWNORMA
}
Thread.Sleep(100);
}
}
}
}