If your task calls any synchronous method that does any kind of I/O or other unspecified action that takes time, then there is no general way to "cancel" it.
Depending on how you try to "cancel" it, one of the following may happen:
- The operation actually gets canceled and the resource it works on is in a stable state (You were lucky!)
- The operation actually gets canceled and the resource it works on is in an inconsistent state (potentially causing all sorts of problems later)
- The operation continues and potentially interferes with whatever your other code is doing (potentially causing all sorts of problems later)
- The operation fails or causes your process to crash.
- You don't know what happens, because it is undocumented
There are valid scenarios where you can and probably should cancel a task using one of the generic methods described in the other answers. But if you are here because you want to interrupt a specific synchronous method, better see the documentation of that method to find out if there is a way to interrupt it, if it has a "timeout" parameter, or if there is an interruptible variation of it.