public ITargetBlock <DateTimeOffset> CreateNeverEndingTaskMakingWcfCalls( Func <DateTimeOffset, CancellationToken, WorkstationMonitorServiceClient, Task> action, CancellationToken cancellationToken, uint betweenCallsDelayInSeconds, string machineIdentifier, WorkstationMonitorServiceClient client) { ActionBlock <DateTimeOffset> block = null; block = new ActionBlock <DateTimeOffset>( async now => { try { await action(now, cancellationToken, client) .ConfigureAwait(false); await Task.Delay(TimeSpan.FromSeconds(betweenCallsDelayInSeconds), cancellationToken) .ConfigureAwait(false); // Post the action back to the block. block.Post(DateTimeOffset.Now); } catch (InvalidOperationException ex) { this.messageSender.SendErrorMessage(ex.Message); } catch (EndpointNotFoundException) { this.messageSender.SendErrorMessageEndpointNotFound(); this.messageSender.SendCancelCommandMessage(machineIdentifier); client.Abort(); } catch (TimeoutException) { this.messageSender.SendErrorMessageTimeout(); this.messageSender.SendCancelCommandMessage(machineIdentifier); client.Abort(); } catch (CommunicationException ex) { this.messageSender.SendErrorMessage(ex.Message); this.messageSender.SendCancelCommandMessage(machineIdentifier); client.Abort(); } catch (TaskCanceledException ex) { client.Abort(); } }, new ExecutionDataflowBlockOptions { CancellationToken = cancellationToken }); return(block); }
public IAsyncCommand CreateRestartMachineOffCommand(UIntParameter timeoutInSeconds) { return(new AsyncCommandParameterized <OperationStatus>( async(cancellationToken, timeout) => { WorkstationMonitorServiceClient workstationMonitorServiceClient = null; OperationStatus result = null; try { workstationMonitorServiceClient = await this.wcfClient.GetNewWorkstationMonitorServiceClient(); result = await this.wcfClient.RestartMachineAsync(workstationMonitorServiceClient, ((UIntParameter)timeout).Parameter) .WithCancellation(cancellationToken) // Following statements will be processed in the same thread, won't use caught context (UI) .ConfigureAwait(false); if (cancellationToken.IsCancellationRequested) { workstationMonitorServiceClient.Abort(); } else { workstationMonitorServiceClient.Close(); } if (result != null && !cancellationToken.IsCancellationRequested) { this.ShowMessageBox(result); } } catch (InvalidOperationException ex) { this.messageSender.SendErrorMessage(ex.Message); // Rethrow exception in order to set correct Task state (Faulted) throw; } catch (EndpointNotFoundException) { this.messageSender.SendErrorMessageEndpointNotFound(); this.messageSender.SendCancelCommandMessage(this.wcfClient.MachineIdentifier); workstationMonitorServiceClient.Abort(); throw; } catch (TimeoutException) { this.messageSender.SendErrorMessageTimeout(); this.messageSender.SendCancelCommandMessage(this.wcfClient.MachineIdentifier); workstationMonitorServiceClient.Abort(); throw; } catch (CommunicationException ex) { this.messageSender.SendErrorMessage(ex.Message); this.messageSender.SendCancelCommandMessage(this.wcfClient.MachineIdentifier); workstationMonitorServiceClient.Abort(); throw; } return result; }, timeoutInSeconds)); }
public IAsyncCommand CreateSoftwareStaticDataCommand( WpfObservableRangeCollection <CurrentUser> currentUser, WpfObservableRangeCollection <OS> operatingSystem, WpfObservableRangeCollection <Bios> bios, WpfObservableRangeCollection <InstalledProgram> installedProgram, WpfObservableRangeCollection <MicrosoftWindowsUpdate> microsoftWindowsUpdate, WpfObservableRangeCollection <StartupCommand> startupCommand, WpfObservableRangeCollection <LocalUser> localUser) { return(new AsyncCommand <SoftwareStaticData>(async(cancellationToken) => { return await Task.Run(async() => { WorkstationMonitorServiceClient workstationMonitorServiceClient = null; SoftwareStaticData result = null; try { workstationMonitorServiceClient = await this.wcfClient.GetNewWorkstationMonitorServiceClient(); result = await this.wcfClient.ReadSoftwareStaticDataAsync(workstationMonitorServiceClient) .WithCancellation(cancellationToken) // Following statements will be processed in the same thread, won't use caught context (UI) .ConfigureAwait(false); if (cancellationToken.IsCancellationRequested) { workstationMonitorServiceClient.Abort(); } else { workstationMonitorServiceClient.Close(); } if (result != null && !cancellationToken.IsCancellationRequested) { currentUser.ReplaceRange(result.CurrentUser, new CurrentUserStaticComparer()); operatingSystem.ReplaceRange(result.OperatingSystem, new OSComparer()); bios.ReplaceRange(result.Bios, new BiosComparer()); installedProgram.ReplaceRange(result.InstalledProgram, new InstalledProgramComparer()); microsoftWindowsUpdate.ReplaceRange(result.MicrosoftWindowsUpdate, new MicrosoftWindowsUpdateComparer()); startupCommand.ReplaceRange(result.StartupCommand, new StartupCommandComparer()); localUser.ReplaceRange(result.LocalUser, new LocalUserComparer()); } } catch (InvalidOperationException ex) { this.messageSender.SendErrorMessage(ex.Message); // Rethrow exception in order to set correct Task state (Faulted) throw; } catch (EndpointNotFoundException) { this.messageSender.SendErrorMessageEndpointNotFound(); this.messageSender.SendCancelCommandMessage(this.wcfClient.MachineIdentifier); workstationMonitorServiceClient.Abort(); throw; } catch (TimeoutException) { this.messageSender.SendErrorMessageTimeout(); this.messageSender.SendCancelCommandMessage(this.wcfClient.MachineIdentifier); workstationMonitorServiceClient.Abort(); throw; } catch (CommunicationException ex) { this.messageSender.SendErrorMessage(ex.Message); this.messageSender.SendCancelCommandMessage(this.wcfClient.MachineIdentifier); workstationMonitorServiceClient.Abort(); throw; } return result; }); })); }
public IAsyncCommand CreateHardwareStaticDataCommand( WpfObservableRangeCollection <ProcessorStatic> processorStatic, WpfObservableRangeCollection <ProcessorCache> processorCache, WpfObservableRangeCollection <Memory> memoryItems, WpfObservableRangeCollection <BaseBoard> baseBoard, WpfObservableRangeCollection <VideoController> videoController, WpfObservableRangeCollection <NetworkAdapter> networkAdapter, WpfObservableRangeCollection <PnPEntity> pnPEntity, WpfObservableRangeCollection <CDROMDrive> cDROMDrive, WpfObservableRangeCollection <Fan> fan, WpfObservableRangeCollection <Printer> printer, WpfObservableRangeCollection <Battery> battery, WpfObservableRangeCollection <Storage> storage) { return(new AsyncCommand <HardwareStaticData>(async(cancellationToken) => { WorkstationMonitorServiceClient workstationMonitorServiceClient = null; HardwareStaticData result = null; try { workstationMonitorServiceClient = await this.wcfClient.GetNewWorkstationMonitorServiceClient(); result = await Task.Run <HardwareStaticData>(() => workstationMonitorServiceClient.ReadHardwareStaticDataAsync()) .WithCancellation(cancellationToken) // Following statements will be processed in the same thread, won't use caught context (UI) .ConfigureAwait(false); if (cancellationToken.IsCancellationRequested) { workstationMonitorServiceClient.Abort(); } else { workstationMonitorServiceClient.Close(); } if (result != null && !cancellationToken.IsCancellationRequested) { processorStatic.ReplaceRange(result.Processor, new ProcessorStaticComparer()); processorCache.ReplaceRange(result.ProcessorCache, new ProcessorCacheStaticComparer()); memoryItems.ReplaceRange(result.Memory, new MemoryStaticComparer()); baseBoard.ReplaceRange(result.BaseBoard, new BaseBoardStaticComparer()); videoController.ReplaceRange(result.VideoController, new VideoControllerStaticComparer()); networkAdapter.ReplaceRange(result.NetworkAdapter, new NetworkAdapterStaticComparer()); pnPEntity.ReplaceRange(result.PnPEntity, new PnPEntityStaticComparer()); cDROMDrive.ReplaceRange(result.CDROMDrive, new CDROMDriveStaticComparer()); fan.ReplaceRange(result.Fan, new FanStaticComparer()); printer.ReplaceRange(result.Printer, new PrinterStaticComparer()); battery.ReplaceRange(result.Battery, new BatteryStaticComparer()); storage.ReplaceRange(result.Storage); } } catch (InvalidOperationException ex) { this.messageSender.SendErrorMessage(ex.Message); // Rethrow exception in order to set correct Task state (Faulted) throw; } catch (EndpointNotFoundException) { this.messageSender.SendErrorMessageEndpointNotFound(); this.messageSender.SendCancelCommandMessage(this.wcfClient.MachineIdentifier); workstationMonitorServiceClient.Abort(); throw; } catch (TimeoutException) { this.messageSender.SendErrorMessageTimeout(); this.messageSender.SendCancelCommandMessage(this.wcfClient.MachineIdentifier); workstationMonitorServiceClient.Abort(); throw; } catch (CommunicationException ex) { this.messageSender.SendErrorMessage(ex.Message); this.messageSender.SendCancelCommandMessage(this.wcfClient.MachineIdentifier); workstationMonitorServiceClient.Abort(); throw; } return result; })); }
public IAsyncCommand CreateWindowsLogDynamicDataCommand(WpfObservableRangeCollection <WindowsLog> windowsLog) { return(new AsyncCommand <WindowsLog[]>(async(cancellationToken) => { WorkstationMonitorServiceClient workstationMonitorServiceClient = null; WindowsLog[] result = null; try { workstationMonitorServiceClient = await this.wcfClient.GetNewWorkstationMonitorServiceClient(); result = await this.wcfClient.ReadWindowsLogDynamicDataAsync(workstationMonitorServiceClient) .WithCancellation(cancellationToken) // Following statements will be processed in the same thread, won't use caught context (UI) .ConfigureAwait(false); if (cancellationToken.IsCancellationRequested) { workstationMonitorServiceClient.Abort(); } else { workstationMonitorServiceClient.Close(); } if (result != null && !cancellationToken.IsCancellationRequested) { windowsLog.ReplaceRange(result, new WindowsLogComparer()); } } catch (InvalidOperationException ex) { this.messageSender.SendErrorMessage(ex.Message); // Rethrow exception in order to set correct Task state (Faulted) throw; } catch (EndpointNotFoundException) { this.messageSender.SendErrorMessageEndpointNotFound(); this.messageSender.SendCancelCommandMessage(this.wcfClient.MachineIdentifier); workstationMonitorServiceClient.Abort(); throw; } catch (TimeoutException) { this.messageSender.SendErrorMessageTimeout(); this.messageSender.SendCancelCommandMessage(this.wcfClient.MachineIdentifier); workstationMonitorServiceClient.Abort(); throw; } catch (CommunicationException ex) { this.messageSender.SendErrorMessage(ex.Message); this.messageSender.SendCancelCommandMessage(this.wcfClient.MachineIdentifier); workstationMonitorServiceClient.Abort(); throw; } return result; })); }