private void CopyFile(object data) { lock (this){ try { CopyFileInfo info = data as CopyFileInfo; if (info == null) { return; } string copyFileName = $"{Path.GetFileNameWithoutExtension(info.SourceFile)}_copy_{DateTime.Now:mmss}{Path.GetExtension(info.SourceFile)}"; string destFile = Path.Combine(info.DestFolder, copyFileName); using (FileStream sourceFs = File.OpenRead(info.SourceFile)) { FileStream destFs = new FileStream(destFile, FileMode.OpenOrCreate, FileAccess.Write); destFs.SetLength(sourceFs.Length); long sourceFileLength = sourceFs.Length; byte[] buffer = new byte[1024 * 16]; long readBytes = 0; long totalReadBytes = 0; do { readBytes = sourceFs.Read(buffer, 0, buffer.Length); destFs.Write(buffer, 0, (int)readBytes); totalReadBytes += readBytes; info.Progress = (int)(totalReadBytes / (sourceFileLength / 100.0)); Thread.Sleep(500); } while (readBytes > 0); destFs.Close(); currentThreads.Remove(Thread.CurrentThread); } } catch { } } }
private void Start() { if (countThreads > 0) { for (int i = 1; i <= countThreads; i++) { Thread copyThread = new Thread(CopyFile); var Copy = new CopyFileInfo() { DestFolder = CopyInfo.DestFolder, SourceFile = CopyInfo.SourceFile, Progress = 0 }; threads.Add(Copy); currentThreads.Add(copyThread); copyThread.Start(Copy); IsStarted = true; } } }
public ViewModel() { CopyInfo = new CopyFileInfo(); startCommand = new DelegateCommand(Start, () => IsStarted == false);; fromCommand = new DelegateCommand(SelectFile, () => IsStarted == false); toCommand = new DelegateCommand(SelectFolder, () => IsStarted == false); stopCommand = new DelegateCommand(Stop, () => IsStarted == true); pauseCommand = new DelegateCommand(Pause, () => IsStarted == true); PropertyChanged += (sender, args) => { if (args.PropertyName == nameof(IsStarted)) { startCommand.RaiseCanExecuteChanged(); fromCommand.RaiseCanExecuteChanged(); toCommand.RaiseCanExecuteChanged(); stopCommand.RaiseCanExecuteChanged(); pauseCommand.RaiseCanExecuteChanged(); } }; }