private static void ProcessInput(IBassReplayGainScanner scanner, Stream input, Stream output, out bool exit) { Logger.Write(typeof(BassReplayGainScannerHost), LogLevel.Debug, "Begin reading command."); var command = ReadInput <ScannerCommand>(input); Logger.Write(typeof(BassReplayGainScannerHost), LogLevel.Debug, "Read command: {0}", Enum.GetName(typeof(ScannerCommandType), command.Type)); switch (command.Type) { case ScannerCommandType.Cancel: Logger.Write(typeof(BassReplayGainScannerHost), LogLevel.Debug, "Sending cancellation signal to scanner."); scanner.Cancel(); Logger.Write(typeof(BassReplayGainScannerHost), LogLevel.Debug, "Closing stdin."); input.Close(); exit = true; break; case ScannerCommandType.Quit: Logger.Write(typeof(BassReplayGainScannerHost), LogLevel.Debug, "Closing stdin/stdout."); input.Close(); output.Close(); exit = true; break; default: exit = false; break; } }
public BassReplayGainScannerMonitor(IBassReplayGainScanner scanner, bool reportProgress, CancellationToken cancellationToken) : base(reportProgress) { this.ScannerItems = new Dictionary <Guid, ScannerItem>(); this.Scanner = scanner; this.CancellationToken = cancellationToken; }
private static void ProcessOutput(IBassReplayGainScanner scanner, Stream output) { WriteOutput(output, scanner.ScannerItems); }
private static void Scan(IBassReplayGainScanner scanner, Stream input, Stream output, Stream error) { var thread1 = new Thread(() => { try { Logger.Write(typeof(BassReplayGainScannerHost), LogLevel.Debug, "Starting scanner main thread."); scanner.Scan(); Logger.Write(typeof(BassReplayGainScannerHost), LogLevel.Debug, "Finished scanner main thread."); WriteOutput(output, new ScannerStatus(ScannerStatusType.Complete)); error.Flush(); } catch (Exception e) { Logger.Write(typeof(BassReplayGainScannerHost), LogLevel.Error, "Error on scanner main thread: {0}", e.Message); WriteOutput(output, new ScannerStatus(ScannerStatusType.Error)); new StreamWriter(error).Write(e.Message); error.Flush(); } }) { IsBackground = true }; var thread2 = new Thread(() => { try { Logger.Write(typeof(BassReplayGainScannerHost), LogLevel.Debug, "Starting scanner output thread."); while (thread1.IsAlive) { ProcessOutput(scanner, output); Thread.Sleep(INTERVAL); } ProcessOutput(scanner, output); Logger.Write(typeof(BassReplayGainScannerHost), LogLevel.Debug, "Finished scanner output thread."); } catch (Exception e) { Logger.Write(typeof(BassReplayGainScannerHost), LogLevel.Error, "Error on scanner output thread: {0}", e.Message); } }) { IsBackground = true }; var thread3 = new Thread(() => { var exit = default(bool); try { Logger.Write(typeof(BassReplayGainScannerHost), LogLevel.Debug, "Starting scanner input thread."); while (thread1.IsAlive) { ProcessInput(scanner, input, output, out exit); if (exit) { break; } Thread.Sleep(INTERVAL); } if (!exit) { ProcessInput(scanner, input, output, out exit); } Logger.Write(typeof(BassReplayGainScannerHost), LogLevel.Debug, "Finished scanner input thread."); } catch (Exception e) { Logger.Write(typeof(BassReplayGainScannerHost), LogLevel.Error, "Error on scanner input thread: {0}", e.Message); } }) { IsBackground = true }; thread1.Start(); thread2.Start(); thread3.Start(); thread1.Join(); if (!thread2.Join(TIMEOUT)) { Logger.Write(typeof(BassReplayGainScannerHost), LogLevel.Warn, "Scanner output thread did not complete gracefully, aborting."); thread2.Abort(); } if (!thread3.Join(TIMEOUT)) { Logger.Write(typeof(BassReplayGainScannerHost), LogLevel.Warn, "Scanner input thread did not complete gracefully, aborting."); thread3.Abort(); } }