-1

I have wpf application that run some math operations .

When Start button click i calculate some math, and return value to Gui .

every operation has some loop inside.

I want give the user Cancel click that stop all the calculate immediately.

I saw CancellationTokenSource, I need send via Start method the cancellationTokenSource, then when user click on Cancel I cancel the cancellationTokenSource , and on the operation I need to check if cancellationTokenSource is cancel.

that work for me, the problem is that I need put in every loop on my code(I have some) check if the cancellationTokenSource is cancel, then i need to return/throw exception every time.

That too ugly, is there another way to cancel this operation without check lot of time during the code if cancellationTokenSource is cancel?

  • Using CancellationToken.ThrowIfCancellationRequested instead of checking cancelled flag might make your code a bit less ugly – Evk Dec 26 '17 at 22:43

2 Answers2

-1

Yes, write static method:

public static Cycles
{
    public static While(Func<bool> condition, Action body, CancellationToken token)
    {
        while(!token.IsCancellationRequested && condition())
        {
            body();    
        }
    }
}

Then use it:

var cts = new CancellationTokenSource();
Cycles.While(()=> true, 
             ()=>
             {
                 Console.Write('.');
             }, 
             cts.Token);

...

cts.Cancel(); //called in another thread

This is pretty precise equivalent of:

while(!cts.Token.IsCancellationRequested && true)
{
    Console.Write('.');
}

PS: You can just pass CancellationToken instead of whole CancellationTokenSource.

eocron
  • 6,885
  • 1
  • 21
  • 50
  • Calling your method is *more* code (in addition to being more complex code) than the version that doesn't use it. – Servy Dec 26 '17 at 22:08
  • Can you please explain more? I not get you.. I have 30 loops on when Start button clicked , and I put on every loop check if I get cancel, how your code help me and break all this loop (whenever my code is) imitatively ? thank you!!! @eocron – JORDANgintod Dec 26 '17 at 22:12
  • Can you please provide example? I don't get you too. – eocron Dec 26 '17 at 22:12
  • @eocron just think that button apply loop and after that more loop and more ...(20-30 loops) , some loops apply more loops so i need check is cancel in every loop – JORDANgintod Dec 26 '17 at 22:14
  • This is not an answer. Provide complete explanatory example of your case, so that if other come - they will see good Q/A instead of a bunch of comments about "thinking about". – eocron Dec 26 '17 at 22:18
-1

Checking the CancellationToken in the loop, although ugly, is the best way. Even the static method will perform all the work and then check the token so although it's cool I wouldn't suggest it. No offense to the answer, it's still a nifty method.

Managing threads, although we have come along way, is still messy to some extent and this is one of the places that you can't really avoid. You can be creative and clean it up to the point of just making a simple call within the loop that will trigger the work for you so that it's not as ugly and you could make that method static, I believe that's more logical than the earlier answer but sort of the same idea...

I wish I had a better answer for you; but know you're not alone. Engineers have been doing all kind of tricks to make it better / easier / cleaner but ATM that's about as clean as it gets.

Michael Puckett II
  • 6,586
  • 5
  • 26
  • 46