private async Task CheckAndClean(bool clean) { LogTextBox.Clear(); MainTabControl.SelectedTab = LogsTabPage; var cancellationToken = new CancellationToken(false); var so = ControlsHelper.MainTaskScheduler; var unused = Task.Factory.StartNew(() => { AddLogLine("Enumerating Devices..."); var devices = DeviceDetector.GetDevices(); var offline = devices.Where(x => !x.IsPresent && x.IsRemovable && !x.Description.Contains("RAS Async Adapter")).ToArray(); var problem = devices.Where(x => x.Status.HasFlag(DeviceNodeStatus.DN_HAS_PROBLEM)).Except(offline).ToArray(); var unknown = devices.Where(x => x.Description.Contains("Unknown")).Except(offline).Except(problem).ToArray(); var list = new List <string>(); if (offline.Length > 0) { list.Add(string.Format("{0} offline devices.", offline.Length)); } if (problem.Length > 0) { list.Add(string.Format("{0} problem devices.", problem.Length)); } if (unknown.Length > 0) { list.Add(string.Format("{0} unknown devices.", unknown.Length)); } var message = string.Join("\r\n", list); if (list.Count == 0) { AddLogLine("No offline, problem or unknown devices found."); } else if (clean) { foreach (var item in list) { AddLogLine(item); } var result = DialogResult.No; ControlsHelper.Invoke(new Action(() => { var form = new JocysCom.ClassLibrary.Controls.MessageBoxForm { StartPosition = FormStartPosition.CenterParent }; ControlsHelper.CheckTopMost(form); result = form.ShowForm( "Do you want to remove offline, problem or unknown devices?\r\n\r\n" + message, "Do you want to remove devices?", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); form.Dispose(); })); if (result != DialogResult.Yes) { return; } var devList = new List <DeviceInfo>(); devList.AddRange(offline); devList.AddRange(problem); devList.AddRange(unknown); for (var i = 0; i < devList.Count; i++) { var item = devList[i]; AddLogLine("Removing Device: {0}/{1} - {2}", i + 1, list.Count, item.Description); try { var exception = DeviceDetector.RemoveDevice(item.DeviceId); if (exception != null) { AddLogLine(exception.Message); } //System.Windows.Forms.Application.DoEvents(); } catch (Exception ex) { AddLogLine(ex.Message); } } } AddLogLine("Done"); }, CancellationToken.None, TaskCreationOptions.LongRunning, so).ConfigureAwait(true); }
static void Main(params string[] args) { // ------------------------------------------------ // Administrator commands. // ------------------------------------------------ var executed = ProcessAdminCommands(false, args); // If valid command was executed then... if (executed) { return; } // ------------------------------------------------ //var h = "6000000000aa06402a020c7fc4218f00cd3b5a2cb0ecc03f2a04e80050192273028cfafffefbf006"; //var bytes = HexToBytes(h); //Network.Ip6Header header; //var p = Network.Ip6Header.TryParse(bytes, out header); // Update working directory. var fi = new System.IO.FileInfo(Application.ExecutablePath); System.IO.Directory.SetCurrentDirectory(fi.Directory.FullName); // Load embedded assemblies. AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve); if (!RuntimePolicyHelper.LegacyV2RuntimeEnabledSuccessfully) { // Failed to enable useLegacyV2RuntimeActivationPolicy at runtime. } if (Environment.OSVersion.Version.Major >= 6) { NativeMethods.SetProcessDPIAware(); } Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.ApplicationExit += Application_ApplicationExit; if (!IsDebug) { Application.ThreadException += Application_ThreadException; AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; } // Check if settings file is valid. if (!CheckSettings()) { return; } if (IsDebug) { InitMonitors(); Application.Run(new MainForm()); return; } try { InitMonitors(); Application.Run(new MainForm()); } catch (Exception ex) { var message = ""; MainHelper.AddExceptionMessage(ex, ref message); if (ex.InnerException != null) { MainHelper.AddExceptionMessage(ex.InnerException, ref message); } var box = new JocysCom.ClassLibrary.Controls.MessageBoxForm(); if (message.Contains("Could not load file or assembly 'Microsoft.DirectX")) { message += "===============================================================\r\n"; message += "You can click the link below to download Microsoft DirectX."; box.MainLinkLabel.Text = "http://www.microsoft.com/en-gb/download/details.aspx?id=8109"; box.MainLinkLabel.Visible = true; } var result = box.ShowForm(message, "Exception!", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); if (result == DialogResult.Cancel) { Application.Exit(); } } }