Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I usually place any action that can (realistically) take more than a second or two into a separate task, something like:</p> <pre><code>public interface ITask { void ExecuteTask (ITaskExecutionContext context); void AfterSuccess(ITaskExecutionContext context); void AfterFailure(ITaskExecutionContext context); void AfterAbortion(ITaskExecutionContext context); } </code></pre> <p>I also have an abstraction for running such tasks:</p> <pre><code>public interface ITaskExecutor : IDisposable { void BeginTask(ITask task); void TellTaskToStop(); } </code></pre> <p>One of the implementations of this <code>ITaskExecutor</code> is using the <code>BackgroundWorker</code>:</p> <pre><code>public class BackgroundTaskExecutor : ITaskExecutor { public void BeginTask(ITask task) { this.task = task; worker = new BackgroundWorker (); worker.DoWork += WorkerDoWork; worker.RunWorkerCompleted += WorkerRunWorkerCompleted; worker.WorkerSupportsCancellation = true; worker.RunWorkerAsync(); } ... } </code></pre> <p>I heavily rely on the dependency injection and IoC to wire things together. In the presenter then I just call something like:</p> <pre><code>GoAndDontReturnUntilYouBringMeALotOfMoneyTask task = new GoAndDontReturnUntilYouBringMeALotOfMoneyTask(parameters); taskExecutor.BeginTask(task); </code></pre> <p>Cancel/abort buttons are then wired so they tell the task executor / task to abort.</p> <p>It's actually a bit more complex than presented here, but this is the general idea.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload