protected void ProcessBlock(object i) { int n = (int)i; try { while (InWork) { while (InWork && !ProcessingReady[n].WaitOne(100)) { continue; } lock (InLocker) { if (InputData == null || InputData[n] == null || !InWork) { break; } } SpecifiedProcessBlock(n); lock (ProgressLocker) CurrentPosition += InputData[n].Length; lock (InLocker) InputData[n] = null; Processed[n].Set(); } } catch (Exception ex) { if (!Displayed) { Displayed = true; OnFatalException?.Invoke(ex); } } Processed[n].Set(); }
protected void FireFatalExceptionEvent(Exception exception) { OnFatalException?.Invoke(this, new SqlDependencyFatalExceptionEventArgs(exception)); }
protected void PoolManage() { try { while (InWork) { while (IsReading && !InputReady.WaitOne(100)) { continue; } if (!InWork) { break; } lock (InLocker) { while (InputQueue.Count == 0) { Monitor.Wait(InLocker); } InputData = InputQueue.Dequeue(); } if (InputData == null) { lock (OutLocker) { OutputQueue.Enqueue(null); Monitor.PulseAll(OutLocker); } break; } ResetIn.Release(); lock (OutLocker) OutputData = new byte[ThreadCount][]; for (int i = 0; i < ThreadCount; i++) { ProcessingReady[i].Set(); } while (InWork && !WaitHandle.WaitAll(Processed, 100)) { continue; } lock (OutLocker) { if (!InWork) { OutputData = null; } OutputQueue.Enqueue(OutputData); Monitor.PulseAll(OutLocker); } } } catch (Exception ex) { Displayed = true; OnFatalException?.Invoke(ex); } }
protected void Writer() { byte[][] Block = null; FileStream OutputStream = null; try { OutputStream = new FileStream(OutputPath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite); while (InWork) { lock (OutLocker) { while (OutputQueue.Count == 0) { Monitor.Wait(OutLocker); } Block = OutputQueue.Dequeue(); } if (Block == null) { InWork = false; break; } for (int n = 0; n < ThreadCount; n++) { if (!InWork) { return; } try { if (Block[n] == null) { InWork = false; break; } OutputStream.Write(Block[n], 0, Block[n].Length); UpdateProgress(); } catch (IOException ex) { Displayed = true; if (Display.ShowMessage(ex.ToString() + "\r\n\r\nTry Again? (y/n): ", true)) { Displayed = false; n--; continue; } else { OnFatalException?.Invoke(null); return; } } catch (Exception ex) { Displayed = true; OnFatalException?.Invoke(ex); return; } } Block = null; } } catch (Exception ex) { Displayed = true; OnFatalException?.Invoke(ex); } finally { OutputStream?.Close(); } }
protected void Reader() { byte[][] Block = new byte[ThreadCount][]; try { InputStream = new FileStream(InputPath, FileMode.Open, FileAccess.Read, FileShare.Read); FileSize = InputStream.Length; while (InputStream.Position < InputStream.Length) { while (InWork && !ResetIn.WaitOne(100)) { continue; } if (!InWork) { break; } for (int n = 0; n < ThreadCount; n++) { try { Block[n] = ReadBlock(n); } catch (Exception ex) { Displayed = true; OnFatalException?.Invoke(ex); return; } } lock (InLocker) { InputQueue.Enqueue(Block); Block = new byte[ThreadCount][]; InputReady.Set(); } } lock (InLocker) { InputQueue.Enqueue(null); Monitor.PulseAll(InLocker); InputReady.Set(); } } catch (Exception ex) { Displayed = true; OnFatalException?.Invoke(ex); } finally { InputStream?.Close(); } IsReading = false; }