c# - Running a separate thread, updating the UI and being able to cancel it -


I have a Windows application that runs a process that takes up to 3 hours to complete. When this is going on, I would like the UI to be responsive to the 'stop' command and update on job progress. In simple words, the long-running task is recording thousands of times every time and it is in this loop that I would like to update the UI.

I have been able to run the code using the Content Synchronization contact option. TPL work has also been used to cancel the cancellation of my thread.

Although I can not work to combine both of these

Make sure that what it means with "two combinations" , but here it is here is a very simple example of a long-running job with cancellation support, after each iteration, a UI component Updates

  var tokenSource = new cancellationTokenSource (); Var token = tokenSource.Token; Task.Run ((= (for (int i = 0; i & lt; = 50; i ++) {thread.Sleep (300); // Simulate work // Check if the job has been canceled And throw it if necessary (Token.Ic cancellation requested) Token. Cancel translation (); // Otherwise update some textbox UI. (() => Token); // cancel operation after 5 seconds; Do Task.Run (Async Rep {Task Wait (5000); tokenSource.Cancel ();});  

Comments