public bool Start(string Source, string Destination, bool WaitCompletion) { if (Source == null || Destination == null || !File.Exists(Source)) { return(false); } source = Source; destination = Destination; SizeCopied = 0; SourceInfo = new FileInfo(source); if (File.Exists(destination)) { c_SysIO.DeleteFile(destination); } CircBuf.AbortWait(false); CircBuf.FlushBuffer(); CopyRunning = true; DestinationComplited.Reset(); SourceComplited.Reset(); StartSource.Set(); StartDestination.Set(); if (WaitCompletion) { WaitResult(); } CopyRunning = false; return(true); }
void DestinationWrite_DoWork(object sender, DoWorkEventArgs e) { byte[] TempData = new byte[MaxNumBytesPerTime]; while (!DestinationWrite.CancellationPending) { StartDestination.WaitOne(); long SizeToCopy = SourceInfo.Length; uint SizeReadyToCopy = 0; FileStream DestinationFile = null; try { DestinationFile = new FileStream(destination, FileMode.CreateNew, FileAccess.Write, FileShare.None, MaxNumBytesPerTime, FileOptions.SequentialScan); while (SizeToCopy > 0 && CopyRunning) { long SizeWait = Math.Min(SizeToCopy, (CircBuf.p_BufferSize - MaxNumBytesPerTime)); while ((SizeReadyToCopy = CircBuf.GetFIFOCounterWithWait()) < SizeWait && CopyRunning) { CircBuf.WaitWrite(); } do { SizeReadyToCopy = Math.Min(SizeReadyToCopy, MaxNumBytesPerTime); if (CircBuf.ReadData(ref TempData, 0, SizeReadyToCopy)) { DestinationFile.Write(TempData, 0, (int)SizeReadyToCopy); SizeCopied += SizeReadyToCopy; SizeToCopy -= SizeReadyToCopy; } } while ((SizeReadyToCopy = CircBuf.GetFIFOCounter()) > MaxNumBytesPerTime && CopyRunning); } } catch (Exception ex) { ErrorWorkProc.ErrorProcess(ex); } finally { if (DestinationFile != null) { DestinationFile.Close(); } } // обновляем аттрибуты try { DestinationInfo = new FileInfo(destination); DestinationInfo.Attributes = SourceInfo.Attributes; DestinationInfo.CreationTime = SourceInfo.CreationTime; DestinationInfo.LastAccessTime = SourceInfo.LastAccessTime; DestinationInfo.LastWriteTime = SourceInfo.LastWriteTime; } catch (Exception ex) { ErrorWorkProc.ErrorProcess(ex); } finally { CopyRunning = false; // если ожидается чтение CircBuf.AbortWait(true); DestinationComplited.Set(); } } }