public void CloseAPI()
 {
     if (apiform != null)
     {
         if (apiform.InvokeRequired)
         {
             apiform.Invoke(new MethodInvoker(() =>
             {
                 CloseAPI();
             }));
         }
         else
         {
             apiform.Close();
             apiform.Dispose();
             apiform = null;
         }
     }
 }
 public void Run(ref bool CanPing)
 {
     try
     {
         RunnerState = RunnerState.Connecting;
         this.SetVisible(true);
         this.CloseAPI();
         var pingreply = new Ping().Send(IP);
         if (pingreply.Status == IPStatus.Success)
         {
             CanPing = true;
         }
         else
         {
             CanPing     = false;
             RunnerState = RunnerState.Failed;
             return;
         }
         ManualResetEvent manualresetevent = new ManualResetEvent(false);
         Thread           thread           = new Thread(new ThreadStart(() =>
         {
             try
             {
                 apiform           = new APIForm(manualresetevent);
                 apiform.OnHIDNum += Apiform_OnHIDNum;
                 if (apiform.Connect_Net(IP, Port))
                 {
                     if (apiform.RegEvent(1, 65535))
                     {
                         RunnerState = RunnerState.Success;
                     }
                     else
                     {
                         RunnerState = RunnerState.Failed;
                         manualresetevent.Set();
                         return;
                     }
                 }
                 else
                 {
                     RunnerState = RunnerState.Failed;
                     manualresetevent.Set();
                     return;
                 }
                 Application.Run(apiform);
             }
             catch (Exception ex)
             {
                 RunnerState = RunnerState.Failed;
                 manualresetevent.Set();
                 Logger.Writer.Write(ex);
             }
         }));
         thread.IsBackground = true;
         thread.Start();
         manualresetevent.WaitOne();
         Thread.Sleep(1000);
     }
     catch (Exception ex)
     {
         RunnerState = RunnerState.Failed;
         Logger.Writer.Write(ex);
     }
 }