private void closeHooks()
 {
     if (_hHookBufInfo[0] != (IntPtr)0)
     {
         WinAPI.UnmapViewOfFile(_hHookBufInfo[0]);
         WinAPI.CloseHandle(_hHookBufInfo[0]);
         _hHookBufInfo[0] = (IntPtr)0;
     }
     if (_hHookBufInfo[1] != (IntPtr)0)
     {
         WinAPI.CloseHandle(_hHookBufInfo[1]);
         _hHookBufInfo[1] = (IntPtr)0;
     }
     if (_hHookBufInfo[2] != (IntPtr)0)
     {
         WinAPI.CloseHandle(_hHookBufInfo[2]);
         _hHookBufInfo[2] = (IntPtr)0;
     }
     if (_hHook != 0)
     {
         DllHookUtil.CloseHook(_hHook);
         _hHook = 0;
     }
 }
        protected override void Run()
        {
            ServerProcess = new Process();
            if (!string.IsNullOrEmpty(Options.ExecutablePath))
            {
                FileInfo executablePath = new FileInfo(Options.ExecutablePath);
                if (executablePath.Exists)
                {
                    string injectError            = "";
                    WinAPI.SECURITY_ATTRIBUTES SA = new WinAPI.SECURITY_ATTRIBUTES();
                    SA.nLength              = (uint)(Marshal.SizeOf(typeof(WinAPI.SECURITY_ATTRIBUTES)));
                    SA.bInheritHandle       = true;
                    SA.lpSecurityDescriptor = IntPtr.Zero;
                    IntPtr SAPtr = Marshal.AllocHGlobal(Marshal.SizeOf(SA));
                    Marshal.StructureToPtr(SA, SAPtr, true);

                    _hHookBufInfo[0] = WinAPI.CreateFileMapping((IntPtr)(-1), SAPtr, WinAPI.FileMapProtection.PageReadWrite, 0, 16384, null);
                    _hHookBufInfo[1] = WinAPI.CreateEvent(SAPtr, false, false, null);
                    _hHookBufInfo[2] = WinAPI.CreateEvent(SAPtr, false, false, null);

                    ServerProcess.StartInfo.FileName         = executablePath.FullName;
                    ServerProcess.StartInfo.WorkingDirectory = executablePath.Directory.FullName;
                    ServerProcess.StartInfo.Arguments        = string.Format("{0} -HFILE {1} -HPARENT {2} -HCHILD {3} -conheight 24",
                                                                             Options.CommandLine(),
                                                                             _hHookBufInfo[0],
                                                                             _hHookBufInfo[1],
                                                                             _hHookBufInfo[2]);
                    ServerProcess.StartInfo.UseShellExecute = false;

                    do
                    {
                        ServerProcess.Start();
                        Thread.Sleep(50);
                        try
                        {
                            DllHookUtil.CreateHook(CommonUtils.GetProgramDirectory() + @"\Dlls\ErrorsHook.dll", ref _hHook, ServerProcess.Threads[0].Id);
                        }
                        catch (Exception e)
                        {
                            _logger.DebugException(Properties.Resources.log_UnableToCreateHook, e);
                        }
                        try
                        {
                            ServerProcess.WaitForInputIdle(5000);
                        }
                        catch (Exception e)
                        {
                            _logger.DebugException(Properties.Resources.log_UnableToSetAffinity, e);
                        }
                        DllInjectUtil.DoInject(ServerProcess, CommonUtils.GetProgramDirectory() + @"\Dlls\Inject.dll", out injectError);

                        if ((Options as ValveGameServerOptions).ConsoleType == ConsoleType.Integrated)
                        {
                            WinAPI.ShowWindow(ServerProcess.MainWindowHandle, WinAPI.SW_HIDE);
                        }
                        else
                        {
                            WinAPI.SetWindowPos(ServerProcess.MainWindowHandle,
                                                (IntPtr)WinAPI.HWND_TOP,
                                                (Options as ValveGameServerOptions).ConsolePositionX,
                                                (Options as ValveGameServerOptions).ConsolePositionY,
                                                0, 0, WinAPI.SWP_NOSIZE);
                        }

                        if (Options.ProcessorAffinity != (IntPtr)0)
                        {
                            try
                            {
                                ServerProcess.ProcessorAffinity = Options.ProcessorAffinity;
                            }
                            catch (Exception e)
                            {
                                _logger.DebugException(Properties.Resources.log_UnableToSetAffinity, e);
                            }
                        }
                        try
                        {
                            ServerProcess.PriorityClass = Options.Priority;
                        }
                        catch (Exception e)
                        {
                            _logger.DebugException(Properties.Resources.log_UnableToSetPriority, e);
                        }
                        _refreshInfoThread.Start();
                        _logger.Info(string.Format(Properties.Resources.log_SuccessfulStarted, Options.HostName));
                        ServerStatus.Status = true;
                        OnServerStarted(this, new EventArgs());
                        ServerProcess.WaitForExit();

                        ServerStatus.Status = false;
                        _refreshInfoThread.Abort();
                        _refreshInfoThread = ThreadUtil.GetThread(RefreshText);

                        CrashWithoutDialog();

                        if (!string.IsNullOrWhiteSpace(ServerStatus.Map))
                        {
                            ServerProcess.StartInfo.Arguments = string.Format("{0} -HFILE {1} -HPARENT {2} -HCHILD {3} -conheight 24",
                                                                              Options.CommandLine().Replace((Options as ValveGameServerOptions).Map, ServerStatus.Map),
                                                                              _hHookBufInfo[0],
                                                                              _hHookBufInfo[1],
                                                                              _hHookBufInfo[2]);
                        }

                        ResetStatus();
                        Statistics.AddCrashInfo(ServerStatus.Map);
                        DllHookUtil.CloseHook(_hHook);
                        if (!CanRestart())
                        {
                            _logger.Info(string.Format(Properties.Resources.log_CrashCountMaxLimit, Options.HostName, Options.MaxCrashCount, Options.CrashCountTime));

                            break;
                        }
                    }while (Options.AutoRestart);
                    OnServerStoped(this, new EventArgs());
                }
                else
                {
                    _logger.Warn(string.Format(Properties.Resources.log_StartFailedFileIsNotExists, Options.ExecutablePath));
                }
            }
            else
            {
                _logger.Warn(string.Format(Properties.Resources.log_StartFailedFileIsNotExists, Options.ExecutablePath));
            }
        }