private void Prepare() { rootDir = new DirectoryInfo(txtDest.Text.Trim()); if (!rootDir.Exists) { rootDir.Create(); rootDir.Refresh(); Nira.FixTimestamp(rootDir); } tmpDir = new DirectoryInfo(rootDir.FullName.Substring(0, 3) + $"\\.wtmp..{Guid.NewGuid()}.."); if (!tmpDir.Exists) { tmpDir.Create(); tmpDir.Refresh(); Nira.FixTimestamp(tmpDir); #if !DEBUG tmpDir.Attributes |= System.IO.FileAttributes.Hidden | FileAttributes.System; #endif } fileExt = txtFileExt.Text.Trim(); writeBuffer = new byte[blockZise + 1]; MaxTask = int.Parse(txtTasks.Text.Trim()); }
private bool WriteFileSync(string path) { if (File.Exists(path)) { return(true); } var free = Nira.GetDriveFreeSpace(rootDir.FullName); if (free < blockZise * blockCount) { WriteFile(path, free); return(false); } Dispatcher.Invoke(() => lblFileStat.Content = $"{path} init"); rand.NextBytes(writeBuffer); using (var fs = new FileStream(path, FileMode.OpenOrCreate)) { for (int i = 0; i < blockCount; ++i) { if ((i % 10) == 0) { Dispatcher.Invoke(() => lblFileStat.Content = $"{path} {i}/{blockCount}"); } fs.Write(writeBuffer, 0, writeBuffer.Length); } fs.Flush(); } FixTimestamp(path); return(true); }
private void FixTimestamp(string path) { lock (obzekt) { Nira.FixTimestamp(new FileInfo(path)); Nira.FixTimestamp(rootDir); } }
void UpdateDisplay() { long total = 0; var df = Nira.GetDriveFreeSpace(CurrentDrive, out total); var n = (double)df / total; lubel.Content = $"{CurrentDrive}: {df}/{total} ({100 - (n * 100):F2})"; mrogressBar.Value = (int)(100 - (n * 100)); }
private void WriteFileAsync(string path) { spool.Schedule(token => { bool cancel = false; Dispatcher.Invoke(() => lblFileStat.Content = $"Task: {spool.TaskCount}"); if (File.Exists(path)) { return; } var free = Nira.GetDriveFreeSpace(rootDir.FullName); if (free < blockZise * blockCount) { WriteFile(path, free); return; } rand.NextBytes(writeBuffer); var tmp = tmpDir.FullName + $"\\{Guid.NewGuid().ToString()}...mp4"; using (var fs = new FileStream(tmp, FileMode.OpenOrCreate)) { for (int i = 0; i < blockCount; ++i) { if (token.IsCancellationRequested) { cancel = true; Debug.WriteLine($"CancellationRequested"); break; } fs.Write(writeBuffer, 0, writeBuffer.Length); } fs.Flush(); } File.Move(tmp, path); FixTimestamp(path); return; }, token => { Dispatcher.Invoke(() => lblFileStat.Content = $"Task: {spool.TaskCount}"); }); }
private bool WriteFile(string path) { if (File.Exists(path)) { return(true); } var free = Nira.GetDriveFreeSpace(rootDir.FullName); if (free < blockZise * blockCount) { WriteFile(path, free); return(false); } Dispatcher.Invoke(() => lblFileStat.Content = $"{path} init"); rand.NextBytes(writeBuffer); var tmp = tmpDir.FullName + $"\\{Guid.NewGuid().ToString()}...mp4"; using (var fs = spool.CreateClosableStream(new FileStream(tmp, FileMode.OpenOrCreate))) { fs.Closed += (e, v) => { File.Move(tmp, path); FixTimestamp(path); }; for (int i = 0; i < blockCount; ++i) { if ((i % 10) == 0) { Dispatcher.Invoke(() => lblFileStat.Content = $"{path} {i}/{blockCount}"); } fs.Write(writeBuffer, 0, writeBuffer.Length); } } return(true); }
private void BtnStart_Click(object sender, RoutedEventArgs e) { if (running) { tokenSource.Cancel(); btnStart.IsEnabled = false; return; } btnStart.Content = "Stop"; btnStart.IsEnabled = false; Prepare(); running = true; if (tokenSource == null) { tokenSource = new CancellationTokenSource(); } var token = tokenSource.Token; spool.Omit(); Task.Factory.StartNew(() => { Dispatcher.Invoke(() => btnStart.IsEnabled = true); long n = 0; while (true) { if (token.IsCancellationRequested) { break; } if (MaxTask <= 0) { n++; var f = System.IO.Path.Combine(rootDir.FullName, $"{n:D16}.{fileExt}"); Dispatcher.Invoke(() => lblProgress.Content = $"{f} Free:{Nira.GetDriveFreeSpace(rootDir.FullName)}"); if (File.Exists(f)) { continue; } WriteFile(f); } else { if (spool.TaskCount >= MaxTask) { Thread.Sleep(100); continue; } var dfs = Nira.GetDriveFreeSpace(rootDir.FullName); if (dfs < 10) { break; } n++; var f = System.IO.Path.Combine(rootDir.FullName, $"{n:D16}.{fileExt}"); Dispatcher.Invoke(() => lblProgress.Content = $"{f} Free:{dfs}"); if (File.Exists(f)) { continue; } WriteFileAsync(f); } Thread.Sleep(1); } }, token).ContinueWith(t => { tokenSource.Dispose(); tokenSource = null; running = false; spool.Stop(); spool.Wait(); if (tmpDir.Exists) { tmpDir.Delete(); } Dispatcher.Invoke(() => { btnStart.IsEnabled = true; btnStart.Content = "Start"; }); }); }
private void BtnStart_Click(object sender, RoutedEventArgs e) { if (running) { tokenSource.Cancel(); btnStart.IsEnabled = false; return; } inputFile = new FileInfo(txtSrc.Text.Trim()); if (!inputFile.Exists) { MessageBox.Show($"{txtSrc.Text}がないよう"); return; } Prepare(); progressBar.Maximum = inputFile.Length / blockZise; progressBar.Minimum = 0; btnStart.Content = "Stop"; btnStart.IsEnabled = false; running = true; if (tokenSource == null) { tokenSource = new CancellationTokenSource(); } var token = tokenSource.Token; Task.Factory.StartNew(() => { Dispatcher.Invoke(() => btnStart.IsEnabled = true); long n = 0; while (true) { if (token.IsCancellationRequested) { break; } if (taskCount >= MaxTask) { Thread.Sleep(100); continue; } n++; var f = System.IO.Path.Combine(rootDir.FullName, $"{n:D16}{inputFile.Extension}"); Dispatcher.Invoke(() => { progressBar.Value = 0; lblState.Content = $"{f} Free:{Nira.GetDriveFreeSpace(rootDir.FullName)}"; }); if (File.Exists(f)) { continue; } WriteFile(f, token); Thread.Sleep(1); } }, token).ContinueWith(t => { tokenSource.Dispose(); tokenSource = null; running = false; foreach (var n in Tasks.ToArray()) { var boo = n.Result; } if (tmpDir.Exists) { tmpDir.Delete(); } Dispatcher.Invoke(() => { lblState.Content = "done"; btnStart.IsEnabled = true; btnStart.Content = "Start"; }); }); }
private bool WriteFile(string path, CancellationToken token) { if (File.Exists(path)) { return(true); } var free = Nira.GetDriveFreeSpace(rootDir.FullName); if (free < readBuffer.Length) { return(false); } bool cancel = false; var tmp = tmpDir.FullName + $"\\{Guid.NewGuid().ToString()}...tmp"; var fs = new FileStream(tmp, FileMode.OpenOrCreate); long bc = 0; using (var src = new FileStream(inputFile.FullName, FileMode.Open, FileAccess.Read)) { while (true) { Dispatcher.Invoke(() => progressBar.Value = ++bc); var s = src.Read(readBuffer, 0, readBuffer.Length); if (s <= 0) { break; } if (token.IsCancellationRequested) { cancel = true; break; } fs.Write(readBuffer, 0, s); } } Task <bool> task = null; task = new Task <bool>(() => { Tasks.Add(task); taskCount++; fs.Flush(); fs.Close(); fs.Dispose(); fs = null; if (cancel) { File.Delete(tmp); } else { File.Move(tmp, path); FixTimestamp(path); } return(true); }); task.ContinueWith(x => { taskCount--; Tasks.Remove(task); return(true); }); task.Start(); return(true); }