0

I use WPF to create a window and meanwhile create a new task to implement time-costing job which will also block the task before finishing.

Now I want to use a button that can cancel and exit the blocked task though it's still active. The Microsoft document recommends to use CancellationToken to cancel a task but the problem is I can't check whether the IsCancellationRequested flag is triggered because the blocked task has no loop to detect the flag and can't exit the task normally. I also try to use Thread.Abort() to force close the thread but it causes PlatformNotSupportedException.

So I wonder if there's any method to cancel the blocked task/thread and its child task/thread or if WPF privides any approach to close all the task/thread except for the main UI thread.

Wiktoria Prusik
  • 840
  • 4
  • 15
Sim
  • 11
  • 1
  • 1
    You can't cancel an operation that doesn't support being cancelled but you can ignore any return value it produces and just move on in your other thread. – mm8 Nov 20 '19 at 12:18
  • 1
    Task.Run(() => YourLongRunningFunction).Wait(2000); inside the function make a condition to check a properties for False or true to cancel the task – Erwin Draconis Nov 20 '19 at 12:23
  • You've got a new task. That does some long operation. It also blocks some other task. I'm not clear what that latter task is. Do you need your long task to block whatever you mean by this other task? – Andy Nov 20 '19 at 15:49
  • @mm8 I cancel the task just because the hardware runs in a unexpected way and I still need to restart the same job after cancel the task. If I ignore it, will it cause multi useless thread in the program and stuck the computer funnaly? – Sim Nov 21 '19 at 03:13
  • @ErwinDraconis To check a property needs a while/for loop, right? But the program doesn't have loop to execute the job – Sim Nov 21 '19 at 03:15
  • @Andy The long-operation task blocks only itself and I just want to cancle the long blocked task can be cancel by the main UI thread at any time – Sim Nov 21 '19 at 03:17
  • @Sim can you post your longrunning function ? – Erwin Draconis Nov 21 '19 at 07:27
  • You should post sufficient code so we can reproduce your issue with just that code. Which probably means you have to write some different (but effectively similar) code to what you've got in order to avoid dependencies. Otherwise you're very likely to find your question is closed. As it stands your question is not clear. That means it's not suitable for SO. – Andy Nov 21 '19 at 08:14

1 Answers1

0

You can cancel anywhere that you are waiting on the task, and therefore prevent it blocking anything. This can be done using the Task.Wait overloads which take either a timeout, a cancellation token, or both.

If you're wanting to somehow cancel a task that you threw off into the ether then the only way to do this is to pass it the cancellation token when you construct it and then check this using ThrowIfCancellationRequested or IsCancellationRequested at various points within the method you called within the task.

Found this whilst looking for further information (spoiler, you can't do anything more than described above that is a recommendable coding practice / safe): How do I abort/cancel TPL Tasks?

Ross Gurbutt
  • 969
  • 1
  • 8
  • 15
  • Yeah, I have thought both of taking a timeout and cancellation token.Because the job needs a very long time to complete the timeout would be very large. That means I have to wait for a long time. On the other hand, cancellation token needs a while/for loop in the program to chech the IsCancellationRequested but the program needs no loop to execute the job. – Sim Nov 21 '19 at 03:38