In my C# application (unit test, actually) I want to be able to launch an operation and set a timeout. If the timeout elapses and my operation has not terminated, I want to:
- Throw an exception.
- Include a stack trace of the thread running the operation.
- If reasonably convenient, cancel the operation.
I can accomplish point #1 using the method described in this answer by Lawrence Johnston: https://stackoverflow.com/a/22078975/8557153
How can I accomplish point 2? I have a reference to a Task, but I know that Task objects do not have stack traces per se. A Task just a data structure of some kind. Only threads have stack traces. But is there any way to get a stack trace from the thread running the task, if any? Or will I need to completely rewrite the body of my task to make this possible?
Point 3 is optional. It is acceptable to have the task continue unawaited in the background, but it would be nice to abort it.