public static void ResetDevicesByInstancePatch(string instance_match) { if (instance_match.IsNullOrWhiteSpace()) { return; } Guid guid = Guid.Empty; SafeDeviceInfoSetHandle diSetHandle = NativeMethods.SetupDiGetClassDevs(ref guid, null, IntPtr.Zero, SetupDiGetClassDevsFlags.AllClasses); DeviceInfoData[] diData = GetDeviceInfoData(diSetHandle); if (diData.IsNullOrEmpty()) { return; } int[] indexes = GetIndexesOfInstance(diSetHandle, diData, instance_match); if (indexes.IsNullOrEmpty()) { return; } ExceptionBuffer Exceptions = new ExceptionBuffer(); TasksManager Tasks = new TasksManager(indexes.Length); //int success_counter = 0; foreach (int idx in indexes) { Tasks.Run(() => TryReset(diSetHandle, diData[idx]), "" + idx, true); /* * try * { * ResetDevice(diSetHandle, diData[idx]); ++success_counter; * } * catch (Exception ex) * { * Exceptions.Write(ex); * continue; * }*/ } Tasks.JoinStopAll(100); if (diSetHandle != null) { if (diSetHandle.IsClosed == false) { diSetHandle.Close(); } diSetHandle.Dispose(); } }