static void HideWindow() { Console.Title = "AutoConnectNet"; IntPtr intptr = DLLWrapper.FindWindow("ConsoleWindowClass", "AutoConnectNet"); if (intptr != IntPtr.Zero) { DLLWrapper.ShowWindow(intptr, 0);//隐藏这个窗口 } }
static int MaxConnectTimes = 0; //最大重连次数 static void Main(string[] args) { HideWindow(); MaxConnectTimes = 10; string filepath = System.AppDomain.CurrentDomain.BaseDirectory + "bat\\connect.bat"; while (true) { Thread.Sleep(500); if (!DLLWrapper.InternetGetConnectedState(out n, 0) /*|| n != 81*/)//81貌似是连接的宽带 { if (failedToConnectTimes <= MaxConnectTimes) { ConnectNet(filepath); } else { return; } } } }
/// <summary> /// 通过bat重连 /// </summary> /// <param name="filepath"></param> static void ConnectNet(string filepath) { using (Process proc = new Process()) { proc = new Process(); proc.StartInfo.FileName = filepath; proc.StartInfo.CreateNoWindow = true; proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; proc.Start(); bool result = proc.WaitForExit(10000);//等待10s if (!result || !DLLWrapper.InternetGetConnectedState(out n, 0)) { if (!result) { proc.Close(); } failedToConnectTimes++; } } if (failedToConnectTimes % 5 == 0) { GC.Collect(); } }