Skip to content

Add CancellationToken support to ProgressDialog #53

Closed
@vpenades

Description

@vpenades

I have a job action that is declared like this:

void MyJob(IProgress<int> progress, System.Threading.CancellationToken token)
{
    System.SomeTaskMethod(token);
}

And I want to use it along with ProgressDialog.

ProgressDialog already supports IProgress , but I haven't seen a way to connect the CancellationToken with the ProgressDialog, so right now I'm doing something like this:

var dlg = new Ookii.Dialogs.Wpf.ProgressDialog();
                
dlg.DoWork += (s, e) =>
{
    MyJob( dlg, CancellationToken.None );
};

dlg.ShowDialog();

Solutions:

  • Let ProgressDialog be configured with a System.Threading.CancellationTokenSource, so when the user clicks Cancel, it will propagate the cancellation to the tokens. Also add a CancellationToken to DoWorkEventArgs, so it can be used by the job.
    • if a CancellationSource is not configured, a default one should be created.
  • Add a CancelJob event that can be used like: dlg.CancelJob += (s,e) => tokenSource.Cancel();
  • Add a virtual CancelJob method that can be overriden in a derived class.

Given that DotNet is moving towards Task and async/await, so more and more system methods are using CancellationToken, I would suggest to adopt the first solution.

PR?

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions