private async Task <Device> TryGetDeviceAsync(AccessProvider accessProvider, string hostName, CancellationToken cancellationToken = new CancellationToken(), InformationCategory[] informationCategories = null, params InformationType[] informationTypes) { return(await Task.Run(async() => { try { if (accessProvider != null) { var wmiAccessService = await accessProvider.GetAccessAsync(hostName).ConfigureAwait(false); if (wmiAccessService != null) { return await TryGetDeviceAsync(wmiAccessService, cancellationToken, informationCategories, informationTypes).ConfigureAwait(false); } } } catch (OperationCanceledException operationCanceledException) { LogEventHandler.TaskIncompleted(operationCanceledException); } catch (Exception exception) { var wmiException = new WMIGeneralException(hostName, exception); LogEventHandler.Exception(wmiException); } return null; }, cancellationToken).ConfigureAwait(false)); }
private async Task <Device> TryGetDeviceAsync(WMIAccessService wmiAccessService, CancellationToken cancellationToken = new CancellationToken(), InformationCategory[] informationCategories = null, params InformationType[] informationTypes) { if (wmiAccessService != null) { try { return(await Task.Run(async() => { try { if (wmiAccessService.Connected) { var device = await new Device() .WithWMIAccessService(wmiAccessService) .WithInformationCategories(informationCategories) .WithInformationTypes(informationTypes) .WithQueries(Queries) .InitializeAsync(cancellationToken) .ConfigureAwait(false); return device; } } catch (OperationCanceledException operationCanceledException) { LogEventHandler.TaskIncompleted(operationCanceledException); } catch (Exception exception) { var wmiException = new WMIGeneralException(wmiAccessService.EndPoint, exception); LogEventHandler.Exception(wmiException); } return null; }, cancellationToken).ConfigureAwait(false)); } catch (OperationCanceledException operationCanceledException) { LogEventHandler.TaskIncompleted(operationCanceledException); } catch (Exception) { if (!cancellationToken.IsCancellationRequested) { throw; } } } return(null); }
/// <summary> /// Initialize information gathering, when method is finished, properties of this Device will be filled with the gathered data /// </summary> /// <param name="cancellationToken">To cancel the Tasks started by calling the method</param> /// <returns>Returns awaitable Task with this Device</returns> public async Task <Device> InitializeAsync(CancellationToken cancellationToken = new CancellationToken()) { EvaluateSearchOptions(); var start = DateTime.Now; try { await GetDeviceInformationAsync(cancellationToken).ConfigureAwait(false); } catch (OperationCanceledException operationCanceledException) { LogEventHandler.TaskIncompleted(operationCanceledException); } ExecutionTime = DateTime.Now - start; return(this); }