Do not use the Task
constructor with async
/await
.
In this case, you want to use Task.Run
:
private async void computeStart_Click(object sender, EventArgs e)
{
_computeTokenSource = new CancellationTokenSource();
await Task.Run(() => GenerateAll(_computeTokenSource.Token));
}
private void GenerateAll(CancellationToken token)
{
var total = (long) Math.Pow(36, 6);
var options = new ParallelOptions {CancellationToken = token};
Parallel.For(0, total, options, a => GenerateCodeAndHash());
}
For more information, see my async
/await
intro.