private void ProcessFilesInBackground(IEnumerable <string> files, Action <string, IThreadWorker, ProgressContext> processFile) { WorkerGroup workerGroup = null; int maxConcurrency = Environment.ProcessorCount > 2 ? Environment.ProcessorCount - 1 : 2; progressBackgroundWorker.BackgroundWorkWithProgress( (ProgressContext progress) => { progress.AddItems(files.Count()); using (workerGroup = new WorkerGroup(maxConcurrency, progress)) { foreach (string file in files) { IThreadWorker worker = workerGroup.CreateWorker(); string closureOverCopyOfLoopVariableFile = file; InteractionSafeUi(() => { processFile(closureOverCopyOfLoopVariableFile, worker, progress); }); if (workerGroup.FirstError != FileOperationStatus.Success) { break; } progress.AddItems(-1); } workerGroup.WaitAllAndFinish(); return(workerGroup.FirstError); } }, (FileOperationStatus status) => { }); }
public static void TestFinishInBackground() { bool didComplete = false; ProgressContext progress = new ProgressContext(); progress.Progressing += (object sender2, ProgressEventArgs e2) => { didComplete = true; }; using (ThreadWorker threadWorker = new ThreadWorker(progress)) { threadWorker.Work += (object sender, ThreadWorkerEventArgs e) => { using (WorkerGroup workerGroup = new WorkerGroup(progress)) { IThreadWorker worker = workerGroup.CreateWorker(); worker.Work += (object sender2, ThreadWorkerEventArgs e2) => { e2.Progress.NotifyLevelStart(); e2.Progress.NotifyLevelFinished(); }; worker.Run(); } }; threadWorker.Run(); } Assert.That(didComplete, "Execution should continue here, with the flag set indicating that the progress event occurred."); }
public static void TestSimple() { int workThreadId = -1; FileOperationContext returnedStatus = new FileOperationContext(String.Empty, ErrorStatus.UnspecifiedError); bool done = false; using (IThreadWorker worker = Resolve.Portable.ThreadWorker(nameof(TestSimple), new ProgressContext(), false)) { worker.WorkAsync = (ThreadWorkerEventArgs e) => { workThreadId = Thread.CurrentThread.ManagedThreadId; e.Result = new FileOperationContext(String.Empty, ErrorStatus.Success); return(Task.FromResult <object>(null)); }; worker.Completing += (object sender, ThreadWorkerEventArgs e) => { returnedStatus = e.Result; done = true; }; worker.Run(); worker.Join(); } Assert.That(returnedStatus.ErrorStatus, Is.EqualTo(ErrorStatus.Success), "The status should be returned as successful."); Assert.That(workThreadId, Is.Not.EqualTo(Thread.CurrentThread.ManagedThreadId), "The work should not be performed on the caller thread."); Assert.That(done, Is.True, "The background work must have executed the completed handler now."); }
public static void TestObjectDisposedException() { IThreadWorker worker = Resolve.Portable.ThreadWorker(nameof(TestObjectDisposedException), new ProgressContext(), false); worker.WorkAsync = (ThreadWorkerEventArgs e) => { e.Result = new FileOperationContext(String.Empty, ErrorStatus.Success); return(Task.FromResult <object>(null)); }; try { worker.Run(); worker.Join(); } finally { worker.Dispose(); } bool hasCompleted = false; Assert.Throws <ObjectDisposedException>(() => { worker.Run(); }); Assert.DoesNotThrow(() => { worker.Join(); }); Assert.DoesNotThrow(() => { hasCompleted = worker.HasCompleted; }); Assert.That(hasCompleted, "Even though the thread has completed, the variable should be set, since we allow calls to HasCompleted even after Dispose()."); Assert.DoesNotThrow(() => { worker.Dispose(); }); }
public static void TestProgress() { FakeRuntimeEnvironment environment = (FakeRuntimeEnvironment)OS.Current; int progressCalls = 0; ProgressContext progress = new ProgressContext(); using (IThreadWorker worker = Resolve.Portable.ThreadWorker(nameof(TestProgress), progress, false)) { worker.WorkAsync = (ThreadWorkerEventArgs e) => { environment.CurrentTiming.CurrentTiming = TimeSpan.FromSeconds(1); e.Progress.AddCount(1); e.Result = new FileOperationContext(String.Empty, ErrorStatus.Success); return(Task.FromResult <object>(null)); }; progress.Progressing += (object sender, ProgressEventArgs e) => { ++progressCalls; }; worker.Run(); worker.Join(); } Assert.That(progressCalls, Is.EqualTo(1), "The Progressing event should be raised exactly one time."); }
static void Main(string[] args) { ISingleConsumerQueue <TextFileContext> consumerQueue = new MonitorSingleConsumerQueue <TextFileContext>(new TextFileConsumer(FileName)); IThreadWorker producerThread = new TextFileContextProducer(consumerQueue); IThreadWorker consumerThread = consumerQueue; try { consumerThread.Start(); producerThread.Start(); Console.WriteLine($"({Thread.CurrentThread.Name}): Модель запущена. Данные пишутся в {Path.Combine(Directory.GetCurrentDirectory(), FileName)}"); Console.WriteLine($"({Thread.CurrentThread.Name}): Нажмите любую клавишу, чтобы остановить модель."); Console.ReadKey(true); Console.WriteLine($"({Thread.CurrentThread.Name}): Остановка модели..."); } finally { producerThread.Dispose(); consumerThread.Dispose(); producerThread.Join(); consumerThread.Join(); Console.WriteLine($"({Thread.CurrentThread.Name}): Модель остановлена. Нажмите любую клавишу для выхода..."); Console.ReadKey(true); } }
public static void TestFinishInBackground() { bool didComplete = false; ProgressContext progress = new ProgressContext(); progress.Progressing += (object sender2, ProgressEventArgs e2) => { didComplete = true; }; using (IThreadWorker threadWorker = Resolve.Portable.ThreadWorker(nameof(TestFinishInBackground) + "Outer", progress, false)) { threadWorker.WorkAsync = (ThreadWorkerEventArgs e) => { using (WorkerGroup workerGroup = new WorkerGroup(progress)) { IThreadWorker worker = workerGroup.CreateWorker(nameof(TestFinishInBackground) + "Inner"); worker.WorkAsync = (ThreadWorkerEventArgs e2) => { e2.Progress.NotifyLevelStart(); e2.Progress.NotifyLevelFinished(); return(Task.FromResult <object>(null)); }; worker.Run(); } return(Task.FromResult <object>(null)); }; threadWorker.Run(); threadWorker.Join(); } Assert.That(didComplete, "Execution should continue here, with the flag set indicating that the progress event occurred."); }
public static void TestAddingSubscribersToWorkerThread() { using (WorkerGroup workerGroup = new WorkerGroup()) { IThreadWorker worker = workerGroup.CreateWorker(nameof(TestAddingSubscribersToWorkerThread)); bool wasPrepared = false; worker.Prepare += (object sender, ThreadWorkerEventArgs e) => { wasPrepared = true; }; bool didWork = false; worker.WorkAsync = (ThreadWorkerEventArgs e) => { didWork = true; return(Task.FromResult <object>(null)); }; bool didComplete = false; worker.Completing += (object sender, ThreadWorkerEventArgs e) => { didComplete = true; }; worker.Run(); workerGroup.WaitAllAndFinish(); Assert.That(wasPrepared, "The Prepare event should be raised."); Assert.That(didWork, "The Work event should be raised."); Assert.That(didComplete, "The Completed event should be raised."); } }
public static void TestRemovingSubscribersFromWorkerThread() { using (WorkerGroup workerGroup = new WorkerGroup()) { IThreadWorker worker = workerGroup.CreateWorker(nameof(TestRemovingSubscribersFromWorkerThread) + "1"); worker.Prepare += ThreadWorkerEventHandler; worker.WorkAsync = ThreadWorkerCommandHandler; worker.Completing += ThreadWorkerEventHandler; worker.Run(); workerGroup.WaitAllAndFinish(); Assert.That(workerGroup.FirstError.ErrorStatus, Is.EqualTo(ErrorStatus.UnspecifiedError), "The status should be set by one of the event handlers."); } using (WorkerGroup workerGroup = new WorkerGroup()) { IThreadWorker worker = workerGroup.CreateWorker(nameof(TestRemovingSubscribersFromWorkerThread) + "2"); worker.Prepare += ThreadWorkerEventHandler; worker.WorkAsync = ThreadWorkerCommandHandler; worker.Completing += ThreadWorkerEventHandler; worker.Prepare -= ThreadWorkerEventHandler; worker.WorkAsync = null; worker.Completing -= ThreadWorkerEventHandler; worker.Run(); workerGroup.WaitAllAndFinish(); Assert.That(workerGroup.FirstError.ErrorStatus, Is.EqualTo(ErrorStatus.Success), "None of the event handlers should have been called, so the status should not have been set there."); } }
private void EncryptFile(string file, IThreadWorker worker, ProgressContext progress) { FileOperationsController operationsController = new FileOperationsController(persistentState.Current, progress); operationsController.QuerySaveFileAs += (object sender, FileOperationEventArgs e) => { using (SaveFileDialog sfd = new SaveFileDialog()) { sfd.Title = Resources.EncryptFileSaveAsDialogTitle; sfd.AddExtension = true; sfd.ValidateNames = true; sfd.CheckPathExists = true; sfd.DefaultExt = OS.Current.AxCryptExtension; sfd.FileName = e.SaveFileFullName; sfd.Filter = Resources.EncryptedFileDialogFilterPattern.InvariantFormat(OS.Current.AxCryptExtension); sfd.InitialDirectory = Path.GetDirectoryName(e.SaveFileFullName); sfd.ValidateNames = true; DialogResult saveAsResult = sfd.ShowDialog(); if (saveAsResult != DialogResult.OK) { e.Cancel = true; return; } e.SaveFileFullName = sfd.FileName; } }; operationsController.QueryEncryptionPassphrase += (object sender, FileOperationEventArgs e) => { string passphrase = AskForEncryptionPassphrase(); if (String.IsNullOrEmpty(passphrase)) { e.Cancel = true; return; } e.Passphrase = passphrase; AesKey defaultEncryptionKey = new Passphrase(e.Passphrase).DerivedPassphrase; persistentState.Current.KnownKeys.DefaultEncryptionKey = defaultEncryptionKey; }; operationsController.Completed += (object sender, FileOperationEventArgs e) => { if (e.Status == FileOperationStatus.FileAlreadyEncrypted) { e.Status = FileOperationStatus.Success; return; } if (CheckStatusAndShowMessage(e.Status, e.OpenFileFullName)) { IRuntimeFileInfo encryptedInfo = OS.Current.FileInfo(e.SaveFileFullName); IRuntimeFileInfo decryptedInfo = OS.Current.FileInfo(FileOperation.GetTemporaryDestinationName(e.OpenFileFullName)); ActiveFile activeFile = new ActiveFile(encryptedInfo, decryptedInfo, e.Key, ActiveFileStatus.NotDecrypted, null); persistentState.Current.Add(activeFile); persistentState.Current.Save(); } }; operationsController.EncryptFile(file, worker); }
/// <summary> /// Add new Thread-worker to ThreadController /// </summary> /// <param name="worker">ThreadWorker</param> public void addWorker(IThreadWorker worker) { if (worker != null) { worker.init(); m_workers.Add(worker); m_workCounts++; } }
public static void TestDoubleFinished() { using (WorkerGroup workerGroup = new WorkerGroup()) { IThreadWorker worker = workerGroup.CreateWorker(nameof(TestDoubleFinished)); worker.Run(); workerGroup.WaitAllAndFinish(); Assert.Throws <InvalidOperationException>(() => { workerGroup.WaitAllAndFinish(); }); } }
/// <summary> /// Create a ThreadWorker for background work. Concurrency limitations are effective, so this call may block. /// </summary> /// <remarks> /// Since this call may block, it should not be called from the GUI thread if there is a risk of blocking. /// </remarks> /// <returns></returns> public IThreadWorker CreateWorker(string name, bool startSerializedOnUIThread) { if (_disposed) { throw new ObjectDisposedException("WorkerGroup"); } AcquireOneConcurrencyRight(); IThreadWorker threadWorker = Resolve.Portable.ThreadWorker(name, Progress, startSerializedOnUIThread); threadWorker.Completed += new EventHandler <ThreadWorkerEventArgs>(HandleThreadWorkerCompletedEvent); return(new ThreadWorkerWrapper(threadWorker)); }
public static void TestDoubleDispose() { Assert.DoesNotThrow(() => { using (WorkerGroup workerGroup = new WorkerGroup()) { IThreadWorker worker = workerGroup.CreateWorker(nameof(TestDoubleDispose)); worker.Run(); workerGroup.Dispose(); } }); }
public static void TestCoreFunctionality() { int threadCount = 0; int maxCount = 0; using (WorkerGroup workerGroup = new WorkerGroup()) { object threadLock = new object(); IThreadWorker worker1 = workerGroup.CreateWorker(nameof(TestCoreFunctionality) + "1"); worker1.WorkAsync = (ThreadWorkerEventArgs e) => { lock (threadLock) { ++threadCount; if (threadCount > maxCount) { maxCount = threadCount; } } Thread.Sleep(new TimeSpan(0, 0, 0, 0, 100)); return(Task.FromResult <object>(null)); }; worker1.Completing += (object sender, ThreadWorkerEventArgs e) => { --threadCount; }; worker1.Run(); IThreadWorker worker2 = workerGroup.CreateWorker(nameof(TestCoreFunctionality) + "2"); worker2.WorkAsync = (ThreadWorkerEventArgs e) => { lock (threadLock) { ++threadCount; if (threadCount > maxCount) { maxCount = threadCount; } } Thread.Sleep(new TimeSpan(0, 0, 0, 0, 100)); return(Task.FromResult <object>(null)); }; worker2.Completing += (object sender, ThreadWorkerEventArgs e) => { --threadCount; }; worker2.Run(); workerGroup.WaitAllAndFinish(); Assert.That(maxCount, Is.EqualTo(1), "There should never be more than one thread active at one time."); } }
public static void TestObjectDisposed() { WorkerGroup workerGroup = new WorkerGroup(); IThreadWorker worker = workerGroup.CreateWorker(nameof(TestObjectDisposed)); worker.Run(); workerGroup.Dispose(); worker = null; Assert.Throws <ObjectDisposedException>(() => { worker = workerGroup.CreateWorker(nameof(TestObjectDisposed)); }, "A call to a method on a disposed object should raise ObjectDisposedException."); Assert.That(worker, Is.Null, "The worker should still be null, since the previous attempt to create should fail with an exception."); Assert.Throws <ObjectDisposedException>(() => { workerGroup.WaitAllAndFinish(); }, "A call to a method on a disposed object should raise ObjectDisposedException."); }
public static void TestInvalidOperationException() { using (WorkerGroup workerGroup = new WorkerGroup()) { IThreadWorker worker = workerGroup.CreateWorker(nameof(TestInvalidOperationException)); bool?f = null; Assert.Throws <InvalidOperationException>(() => { f = worker.HasCompleted; }); Assert.That(!f.HasValue, "No value should be set, since an exception should have occurred."); Assert.Throws <InvalidOperationException>(() => { worker.Join(); }); worker.Abort(); } }
public static void TestCoreFunctionality() { int threadCount = 0; int maxCount = 0; using (WorkerGroup workerGroup = new WorkerGroup()) { object threadLock = new object(); IThreadWorker worker1 = workerGroup.CreateWorker(); worker1.Work += (object sender, ThreadWorkerEventArgs e) => { lock (threadLock) { ++threadCount; if (threadCount > maxCount) { maxCount = threadCount; } } Thread.Sleep(100); }; worker1.Completing += (object sender, ThreadWorkerEventArgs e) => { --threadCount; }; worker1.Run(); IThreadWorker worker2 = workerGroup.CreateWorker(); worker2.Work += (object sender, ThreadWorkerEventArgs e) => { lock (threadLock) { ++threadCount; if (threadCount > maxCount) { maxCount = threadCount; } } Thread.Sleep(100); }; worker2.Completing += (object sender, ThreadWorkerEventArgs e) => { --threadCount; }; worker2.Run(); workerGroup.WaitAllAndFinish(); Assert.That(maxCount, Is.EqualTo(1), "There should never be more than one thread active at one time."); } }
public static void TestProgressing() { using (WorkerGroup workerGroup = new WorkerGroup()) { int percent = 0; workerGroup.Progress.Progressing += (object sender, ProgressEventArgs e) => { percent = e.Percent; }; IThreadWorker worker = workerGroup.CreateWorker(nameof(TestProgressing)); worker.Run(); workerGroup.WaitAllAndFinish(); Assert.That(percent, Is.EqualTo(100), "Progress at 100 percent should always be reported when the thread completes."); } }
public static void TestPrepare() { bool wasPrepared = false; using (IThreadWorker worker = Resolve.Portable.ThreadWorker(nameof(TestPrepare), new ProgressContext(), false)) { worker.Prepare += (object sender, ThreadWorkerEventArgs e) => { wasPrepared = true; }; worker.Run(); worker.Join(); } Assert.That(wasPrepared, Is.True, "The Prepare event should be raised."); }
private void DecryptFile(string file, IThreadWorker worker, ProgressContext progress) { FileOperationsController operationsController = new FileOperationsController(persistentState.Current, progress); operationsController.QueryDecryptionPassphrase += HandleQueryDecryptionPassphraseEvent; operationsController.QuerySaveFileAs += (object sender, FileOperationEventArgs e) => { string extension = Path.GetExtension(e.SaveFileFullName); using (SaveFileDialog sfd = new SaveFileDialog()) { sfd.AddExtension = !String.IsNullOrEmpty(extension); sfd.CheckPathExists = true; sfd.DefaultExt = extension; sfd.Filter = Resources.DecryptedSaveAsFileDialogFilterPattern.InvariantFormat(extension); sfd.InitialDirectory = Path.GetDirectoryName(file); sfd.FileName = Path.GetFileName(e.SaveFileFullName); sfd.OverwritePrompt = true; sfd.RestoreDirectory = true; sfd.Title = Resources.DecryptedSaveAsFileDialogTitle; DialogResult result = sfd.ShowDialog(); if (result != DialogResult.OK) { e.Cancel = true; return; } e.SaveFileFullName = sfd.FileName; } return; }; operationsController.KnownKeyAdded += (object sender, FileOperationEventArgs e) => { persistentState.Current.KnownKeys.Add(e.Key); }; operationsController.Completed += (object sender, FileOperationEventArgs e) => { if (CheckStatusAndShowMessage(e.Status, e.OpenFileFullName)) { persistentState.Current.RemoveRecentFiles(new string[] { e.OpenFileFullName }, progress); } }; operationsController.DecryptFile(file, worker); }
public static void TestExplicitConstructor() { ProgressContext progress = new ProgressContext(); int percent = 0; progress.Progressing += (object sender, ProgressEventArgs e) => { percent = e.Percent; }; using (WorkerGroup workerGroup = new WorkerGroup(1, progress)) { IThreadWorker worker = workerGroup.CreateWorker(nameof(TestExplicitConstructor)); worker.Run(); workerGroup.WaitAllAndFinish(); } Assert.That(percent, Is.EqualTo(100), "Progress at 100 percent should always be reported when the thread completes."); }
private void DoFile(string fullName, IThreadWorker worker, Func <string, bool> preparation, Func <bool> operation) { if (!preparation(fullName)) { worker.Abort(); OnCompleted(_eventArgs); return; } worker.Work += (object workerSender, ThreadWorkerEventArgs threadWorkerEventArgs) => { operation(); }; worker.Completing += (object workerSender, ThreadWorkerEventArgs threadWorkerEventArgs) => { _eventArgs.Status = threadWorkerEventArgs.Result; OnCompleted(_eventArgs); }; worker.Run(); }
public static void TestExceptionDuringWork() { FileOperationContext status = new FileOperationContext(String.Empty, ErrorStatus.Unknown); using (IThreadWorker worker = Resolve.Portable.ThreadWorker(nameof(TestExceptionDuringWork), new ProgressContext(), false)) { worker.WorkAsync = (e) => { throw new InvalidOperationException("Something went intentionally wrong."); }; worker.Completed += (sender, e) => { status = e.Result; }; worker.Run(); worker.Join(); } Assert.That(status.ErrorStatus, Is.EqualTo(ErrorStatus.Exception)); }
public static void TestErrorSetInWorkCompleted() { bool errorInWork = false; using (IThreadWorker worker = Resolve.Portable.ThreadWorker(nameof(TestErrorSetInWorkCompleted), new ProgressContext(), false)) { worker.WorkAsync = (ThreadWorkerEventArgs e) => { throw new InvalidOperationException(); }; worker.Completing += (object sender, ThreadWorkerEventArgs e) => { errorInWork = e.Result.ErrorStatus == ErrorStatus.Exception; }; worker.Run(); worker.Join(); } Assert.That(errorInWork, Is.True, "The operation was interrupted by an exception and should return status as such."); }
public static void TestCancellationByException() { bool wasCanceled = false; using (IThreadWorker worker = Resolve.Portable.ThreadWorker(nameof(TestCancellationByException), new ProgressContext(), false)) { worker.WorkAsync = (ThreadWorkerEventArgs e) => { throw new OperationCanceledException(); }; worker.Completing += (object sender, ThreadWorkerEventArgs e) => { wasCanceled = e.Result.ErrorStatus == ErrorStatus.Canceled; }; worker.Run(); worker.Join(); } Assert.That(wasCanceled, Is.True, "The operation was canceled and should return status as such."); }
private void WipeFile(string file, IThreadWorker worker, ProgressContext progress) { FileOperationsController operationsController = new FileOperationsController(persistentState.Current, progress); operationsController.WipeQueryConfirmation += (object sender, FileOperationEventArgs e) => { using (ConfirmWipeDialog cwd = new ConfirmWipeDialog()) { cwd.FileNameLabel.Text = Path.GetFileName(file); DialogResult confirmResult = cwd.ShowDialog(); e.ConfirmAll = cwd.ConfirmAllCheckBox.Checked; if (confirmResult == DialogResult.Yes) { e.Skip = false; } if (confirmResult == DialogResult.No) { e.Skip = true; } if (confirmResult == DialogResult.Cancel) { e.Cancel = true; } } }; operationsController.Completed += (object sender, FileOperationEventArgs e) => { if (CheckStatusAndShowMessage(e.Status, e.OpenFileFullName)) { if (!e.Skip) { persistentState.Current.RemoveRecentFiles(new string[] { e.SaveFileFullName }, progress); } } }; operationsController.WipeFile(file, worker); }
public static void TestHasCompleted() { using (IThreadWorker worker = Resolve.Portable.ThreadWorker(nameof(TestHasCompleted), new ProgressContext(), false)) { bool wasCompletedInWork = false; worker.WorkAsync = (ThreadWorkerEventArgs e) => { wasCompletedInWork = worker.HasCompleted; return(Task.FromResult <object>(null)); }; bool wasCompletedInCompleted = false; worker.Completing += (object sender, ThreadWorkerEventArgs e) => { wasCompletedInCompleted = worker.HasCompleted; }; worker.Run(); worker.Join(); Assert.That(!wasCompletedInWork, "Completion is not set as true in the work event."); Assert.That(!wasCompletedInCompleted, "Completion is not set as true until after the completed event."); Assert.That(worker.HasCompleted, "Completion should be set as true when the thread is joined."); } }
private void OpenEncrypted(string file, IThreadWorker worker, ProgressContext progress) { FileOperationsController operationsController = new FileOperationsController(persistentState.Current, progress); operationsController.QueryDecryptionPassphrase += HandleQueryDecryptionPassphraseEvent; operationsController.KnownKeyAdded += (object sender, FileOperationEventArgs e) => { persistentState.Current.KnownKeys.Add(e.Key); }; operationsController.Completed += (object sender, FileOperationEventArgs e) => { if (e.Status == FileOperationStatus.Canceled) { return; } CheckStatusAndShowMessage(e.Status, e.OpenFileFullName); }; operationsController.DecryptAndLaunch(file, worker); }
public static void TestCancellationByRequest() { bool wasCanceled = false; FakeRuntimeEnvironment environment = (FakeRuntimeEnvironment)OS.Current; using (IThreadWorker worker = Resolve.Portable.ThreadWorker(nameof(TestCancellationByRequest), new CancelProgressContext(new ProgressContext()), false)) { worker.WorkAsync = (ThreadWorkerEventArgs e) => { e.Progress.Cancel = true; environment.CurrentTiming.CurrentTiming = TimeSpan.FromSeconds(1); e.Progress.AddCount(1); return(Task.FromResult <object>(null)); }; worker.Completing += (object sender, ThreadWorkerEventArgs e) => { wasCanceled = e.Result.ErrorStatus == ErrorStatus.Canceled; }; worker.Run(); worker.Join(); } Assert.That(wasCanceled, Is.True, "The operation was canceled and should return status as such."); }
/// <summary> /// Process a file with the main task performed in a background thread. /// </summary> /// <param name="fullName"></param> public void EncryptFile(string fullName, IThreadWorker worker) { DoFile(fullName, worker, EncryptFilePreparation, EncryptFileOperation); }
public ThreadWorkerWrapper(IThreadWorker worker) { _worker = worker; }
/// <summary> /// Wipes a file asynchronously on a background thread. /// </summary> /// <param name="fullName">The full name and path of the file to wipe.</param> /// <param name="worker">The worker thread instance on which to do the wipe.</param> public void WipeFile(string fullName, IThreadWorker worker) { DoFile(fullName, worker, WipeFilePreparation, WipeFileOperation); }
private void DoFile(string fullName, IThreadWorker worker, Func<string, bool> preparation, Func<bool> operation) { if (!preparation(fullName)) { worker.Abort(); OnCompleted(_eventArgs); return; } worker.Work += (object workerSender, ThreadWorkerEventArgs threadWorkerEventArgs) => { operation(); }; worker.Completing += (object workerSender, ThreadWorkerEventArgs threadWorkerEventArgs) => { _eventArgs.Status = threadWorkerEventArgs.Result; OnCompleted(_eventArgs); }; worker.Run(); }
/// <summary> /// Decrypt a file, and launch the associated application raising events as required by /// the situation. The decryption is performed asynchronously on a background thread. /// </summary> /// <param name="sourceFile">The full path to an encrypted file.</param> /// <param name="worker">The worker thread on which to execute the decryption and launch.</param> public void DecryptAndLaunch(string fullName, IThreadWorker worker) { DoFile(fullName, worker, DecryptAndLaunchPreparation, DecryptAndLaunchFileOperation); }