public void Send(string archiveFilename, CancellationToken cancellationToken, SimpleProgressCallback progressCallback = null) { // create drop effect memory stream byte[] moveEffect = new byte[] { (byte) (performCut ? 2 : 5), 0, 0, 0 }; MemoryStream dropEffect = new MemoryStream(); dropEffect.Write(moveEffect, 0, moveEffect.Length); // create file data object DataObject data = new DataObject(); data.SetFileDropList(new StringCollection { archiveFilename }); data.SetData("Preferred DropEffect", dropEffect); // create STA thread that'll work with Clipboard object Thread copyStaThread = new Thread(() => { Clipboard.Clear(); Clipboard.SetDataObject(data, true); }) { Name = "Clipboard copy thread" }; copyStaThread.SetApartmentState(ApartmentState.STA); // start the thread and wait for it to finish copyStaThread.Start(); copyStaThread.Join(); }
public void Archive(string rootPath, string archiveFilename, CancellationToken cancellationToken, SimpleProgressCallback progressCallback = null) { using (ZipFile file = new ZipFile(archiveFilename)) { // add all the files from the report to the zip file file.AddDirectory(rootPath); // subscribe for the archiving progress event file.SaveProgress += (sender, args) => { // check if the cancellation was requested if (cancellationToken.IsCancellationRequested) { args.Cancel = true; return; } if (args.EntriesTotal == 0) { // avoid division by zero return; } progressCallback.TryInvoke((args.EntriesSaved + (args.TotalBytesToTransfer > 0 ? 1.0 * args.BytesTransferred / args.TotalBytesToTransfer : 0.0)) / args.EntriesTotal); }; // safe file to disk file.Save(); cancellationToken.ThrowIfCancellationRequested(); } // report operation finished progressCallback.TryInvoke(1.0); }
public void Report(string rootPath, CancellationToken cancellationToken, SimpleProgressCallback progressCallback = null) { // create report subdirectory string newPath = Path.Combine(rootPath, pathSuffix); Directory.CreateDirectory(newPath); // run reporting code ReportInternal(newPath, cancellationToken, progressCallback); }
public static void TryInvoke(this SimpleProgressCallback callback, double progress) { if (callback == null) { return; } callback(progress); }
private static void CleanupRoot(string rootPath, SimpleProgressCallback progressCallback) { if (!Directory.Exists(rootPath)) { // nothing to remove return; } Directory.Delete(rootPath, true); // notify of the completion progressCallback(1.0); }
public void Report(string rootPath, CancellationToken cancellationToken, SimpleProgressCallback progressCallback = null) { File.WriteAllText(Path.Combine(rootPath, "test1.txt"), "This is a first test file"); string dirName = Directory.CreateDirectory(Path.Combine(rootPath, "testDir")).FullName; cancellationToken.ThrowIfCancellationRequested(); File.WriteAllText(Path.Combine(dirName, "test2.txt"), "This is a second test file"); if (ShouldFail) { throw new InvalidOperationException("Fail requested"); } }
public void Report(string rootPath, CancellationToken cancellationToken, SimpleProgressCallback progressCallback = null) { // get a file name for the report string filename = Path.Combine(rootPath, GetFileNameFromTime(timeProvider.GetTime())); // create report file try { File.CreateText(filename).Close(); } catch (IOException ex) { throw new IOException(string.Format("Failed to create report file \"{0}\". See the inner exceptions for details", filename), ex); } // report progress progressCallback.TryInvoke(1d); }
protected override void ReportInternal(string path, CancellationToken cancellationToken, SimpleProgressCallback progressCallback = null) { for (int i = 0; i < aliases.Length; i++) { string alias = aliases[i]; // create a file name for the report string filename = Path.Combine(path, string.Format("{0}.html", alias)); // add some compativility code File.WriteAllText(filename, "<style>span { display: block; }</style>\n"); // dump report file ReporterUtils.DumpOutputToFile("wmic", string.Format("{0} get /format:htable", alias), filename, true); // report progress progressCallback.TryInvoke((i+1d) / aliases.Length); // exit, if cancellation was requested cancellationToken.ThrowIfCancellationRequested(); } }
public void Send(string archiveFilename, CancellationToken cancellationToken, SimpleProgressCallback progressCallback = null) { // just store the archive file name for verification ArchiveFilename = archiveFilename; }
protected override void ReportInternal(string path, CancellationToken cancellationToken, SimpleProgressCallback progressCallback = null) { List <ThreadDump> listDumps = CreateDumps(true); for (int d = 0; d < listDumps.Count; d++) { var dump = listDumps[d]; // create a file name for the report string filename = Path.Combine(path, string.Format("{0}.txt", dump.ManagedThreadId)); StringBuilder strReport = new StringBuilder(); strReport.AppendLine(dump.ManagedThreadId.ToString()); // thread id strReport.AppendLine(dump.Name); // thread name StackTrace stack = dump.StackTrace; // thread stack trace if (stack != null) { for (int f = 0; f < stack.FrameCount; f++) { strReport.AppendLine(stack.GetFrame(f).ToString()); } } File.WriteAllText(filename, strReport.ToString()); // write to file // report progress progressCallback.TryInvoke((d + 1d) / listDumps.Count); // exit, if cancellation was requested cancellationToken.ThrowIfCancellationRequested(); } }
public void Send(string archiveFilename, CancellationToken cancellationToken, SimpleProgressCallback progressCallback = null) { // create drop effect memory stream byte[] moveEffect = new byte[] { (byte)(performCut ? 2 : 5), 0, 0, 0 }; MemoryStream dropEffect = new MemoryStream(); dropEffect.Write(moveEffect, 0, moveEffect.Length); // create file data object DataObject data = new DataObject(); data.SetFileDropList(new StringCollection { archiveFilename }); data.SetData("Preferred DropEffect", dropEffect); // create STA thread that'll work with Clipboard object Thread copyStaThread = new Thread(() => { Clipboard.Clear(); Clipboard.SetDataObject(data, true); }) { Name = "Clipboard copy thread" }; copyStaThread.SetApartmentState(ApartmentState.STA); // start the thread and wait for it to finish copyStaThread.Start(); copyStaThread.Join(); }
/// <summary> /// Implement reporting logic in this method. /// </summary> /// <param name="path">fill directory path including path suffix</param> /// <param name="cancellationToken">cancellation token for the operation</param> /// <param name="progressCallback">callback to execute for progress reports</param> protected abstract void ReportInternal(string path, CancellationToken cancellationToken, SimpleProgressCallback progressCallback = null);
protected override void ReportInternal(string path, CancellationToken cancellationToken, SimpleProgressCallback progressCallback = null) { List<ThreadDump> listDumps = CreateDumps(true); for (int d = 0; d < listDumps.Count; d++) { var dump = listDumps[d]; // create a file name for the report string filename = Path.Combine(path, string.Format("{0}.txt", dump.ManagedThreadId)); StringBuilder strReport = new StringBuilder(); strReport.AppendLine(dump.ManagedThreadId.ToString()); // thread id strReport.AppendLine(dump.Name); // thread name StackTrace stack = dump.StackTrace; // thread stack trace if (stack != null) { for(int f = 0; f < stack.FrameCount; f++) { strReport.AppendLine(stack.GetFrame(f).ToString()); } } File.WriteAllText(filename, strReport.ToString()); // write to file // report progress progressCallback.TryInvoke((d + 1d) / listDumps.Count); // exit, if cancellation was requested cancellationToken.ThrowIfCancellationRequested(); } }
protected override void ReportInternal(string path, CancellationToken cancellationToken, SimpleProgressCallback progressCallback = null) { for (int i = 0; i < aliases.Length; i++) { string alias = aliases[i]; // create a file name for the report string filename = Path.Combine(path, string.Format("{0}.html", alias)); // add some compativility code File.WriteAllText(filename, "<style>span { display: block; }</style>\n"); // dump report file ReporterUtils.DumpOutputToFile("wmic", string.Format("{0} get /format:htable", alias), filename, true); // report progress progressCallback.TryInvoke((i + 1d) / aliases.Length); // exit, if cancellation was requested cancellationToken.ThrowIfCancellationRequested(); } }
public void Send(string archiveFilename, CancellationToken cancellationToken, SimpleProgressCallback progressCallback = null) { File.Delete(archiveFilename); }