0

i try to understand the difference between Tasks and Backgroundworker respectively the using of it.

question 1: what is the difference? it is better management of system-resources? excepting the different syntax.

question2: in a backgroundWorker i can wait inside for a event or its end before jump back (While (bw.isbusy && !bw.CancellationPending)). How to do this in a Task?

question 3: BW.supportscancelation = true. How to do this in a Task?

I lack the perspective here ;-(

Thanks for your support!

Chris

  • 4
    Let `BackgroundWorker` (which is now an *obsolete* class) rest in peace, use `Task`s; `await Task.Delay(milliseconds);` to wait `milliseconds`; `await OtherTask;` - wait for some other task. Use `CancellationTokekSource` and `CancellaionToken` to cancel task execution – Dmitry Bychenko Jul 11 '20 at 11:22
  • Thanks Dmitry! But how i can get; set; CancelationToken. do you have a simple example? –  Jul 12 '20 at 13:17
  • 1
    As a simple demo, let's cancel task (`SomeTask`) if it executes more than `2` seconds: `using (CancellationTokenSource cts = new CancellationTokenSource(2000)) { try { await SomeTask(cts.Token); } catch (TaskCanceledException) { /*SomeTask cancelled*/ }` } – Dmitry Bychenko Jul 12 '20 at 18:50
  • oh! thats all? Thanks! –  Jul 13 '20 at 08:23

0 Answers0