static int Main(string[] args)
        {
            ErrorHandler ReleaseModeErrorHandler;

            Log.Write("Application version {0} start", Tools.CurrentVersion);
            //Set "NOFORM" to better experiment
#if !NOFORM
            if (!DEBUG)
            {
                ReleaseModeErrorHandler              = new ErrorHandler();
                ReleaseModeErrorHandler.ErrorReport += delegate(Exception ex, ErrorHandlerEventArgs e)
                {
                    Log.Write("Sending error report");
                    var    Msg         = ErrorHandler.generateReport(ex);
                    var    MainForm    = Application.OpenForms.OfType <frmMain>().FirstOrDefault();
                    byte[] CurrentFile = null;
                    if (MainForm != null && MainForm.HasFileOpen)
                    {
                        var F = MainForm.CurrentFile;
                        using (var MS = new MemoryStream())
                        {
                            F.Export(MS);
                            MS.Position = 0;
                            CurrentFile = Compression.CompressData(MS.ToArray());
                        }
                    }
                    //Send report
                    e.Handled = ReleaseModeErrorHandler.UploadReport(Msg, CurrentFile == null ? new byte[0] : CurrentFile);
                };
            }
            //Perform update automatically if it's pending
            if (!DEBUG && args.Length == 0 && File.Exists(UpdateHandler.DefaultUpdateExecutable))
            {
                if (UpdateHandler.PerformUpdate())
                {
                    Log.Write("Application version {0} end (Update success)", Tools.CurrentVersion);
                    Log.Close();
                    return(RET.SUCCESS);
                }
            }
            //Only run main application part if no update was performed
            if (DEBUG || !UpdateHandler.Update())
            {
                //Remove console handle to not block any scripts.
                Tools.FreeConsole();
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new frmMain(args.FirstOrDefault()));
            }
            Log.Write("Application version {0} end", Tools.CurrentVersion);
            Log.Close();
            return(RET.SUCCESS);
#else
            Log.Write("Running in test mode");
            //Allocate console or the Console.ReadKey() will crash
            Tools.AllocConsole();

            Console.Error.WriteLine("Arguments: {0}", string.Join("\r\n", args));

            Test();
            Console.Error.WriteLine("#END");
            return(Exit(RET.SUCCESS));
#endif
        }