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

112 lines
4.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
namespace AppServerMobile
{
class Program
{
public static Config CFG;
static MainForm MainWindow = null;
static void Main(string[] args)
{
Console.Title = "AppServerMobileConsole";
if (args.Length > 0)
{
if (args[0] != "-c")
{
// hide the console window
setConsoleWindowVisibility(false, Console.Title);
}
}
else
{
setConsoleWindowVisibility(false, Console.Title);
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
MainWindow = new MainForm();
Application.Run(MainWindow);
/* Console.WriteLine("--------AppServer MOBILE!!! v0.1---------");
Console.WriteLine("--------www.safemobile.com---------------");
CFG = new Config();
AndroidHandler aHandler = new AndroidHandler();
aHandler.Start();
MessageBussHandler mHandler = new MessageBussHandler(CFG.msgBusIP, CFG.msgBusPort);*/
Console.ReadKey();
}
static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
try
{
/*string test2 = "#210#4#AppServer stoped#";
String cmdok2 = "#" + "0.0" + test2; Int32 tmp2 = cmdok2.Length + 1; tmp2 += tmp2.ToString().Length; cmdok2 = "#" + tmp2.ToString() + cmdok2;
System.Text.Encoding enc2 = System.Text.Encoding.ASCII;
//SM.Debug("TX:" + cmdok2); ///maybe console is dead
byte[] buf2 = enc2.GetBytes(cmdok2);
MainWindow.m.mListener.udp.Send(buf2, buf2.Length);*/
}
catch (Exception ex2)
{
//SM.Debug("Error on sending Message to SD that APPServer has problem: " + ex2.ToString());
}
MessageBox.Show(e.Exception.Message, "ThreadException AppServer.Please restart the application.");
// here you can log the exception ...
}
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
try
{
/*string test2 = "#210#4#AppServer stoped#";
String cmdok2 = "#" + "0.0" + test2; Int32 tmp2 = cmdok2.Length + 1; tmp2 += tmp2.ToString().Length; cmdok2 = "#" + tmp2.ToString() + cmdok2;
System.Text.Encoding enc2 = System.Text.Encoding.ASCII;
//SM.Debug("TX:" + cmdok2); ///maybe console is dead
byte[] buf2 = enc2.GetBytes(cmdok2);
MainWindow.m.mListener.udp.Send(buf2, buf2.Length);*/
}
catch (Exception ex2)
{
//SM.Debug("Error on sending Message to SD that APPServer has problem: " + ex2.ToString());
}
MessageBox.Show((e.ExceptionObject as Exception).Message, "Unhandled UI Exception AppServer.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);
}
}
}
}