I have some business logic that does some long calculations, returns a result and doesn't accept any CancellationTokenSources and, accordingly, doesn't check for it's isCancellationRequested property.
public class Result
{
}
public class LongTask
{
public Result Run()
{
Thread.Sleep(10000);
return new Result();
}
}
I want my UI (MAUI if it matters) to not only run that business logic and update let's say some Label with recieved result asynchronously, but also have ability to cancel that operation.
So how do I do that? Is that even possible without changing the business logic? Please feel free to ask for details if you need to.
More info: LongTask.Run performs CPU-intensive benchmark. User may enter benchmark parameters that will result in very long waiting so I want user to be able to cancel that operation. I target .NET 7 platform.
Best practice would still be to change the business logic and use the CancellationToken Struct. But what you could do is add a count and when that count is reached then return the Task.