Click here to Skip to main content
15,893,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i want cancel current task run by click on another button
how?

What I have tried:

private async void Search_Button_ClickAsync(object sender, EventArgs e)
        {
             await (Task.Run(() =>
			 //... my program
			 ));
        }

private async void CancelButton_ClickAsync(object sender, EventArgs e)
        {
// i want cancel by click this button
        }		
Posted
Updated 9-May-18 20:20pm
v3

You need to check within your method to see if cancellation has been requested. Here is a snippet from a ViewModel to show the sort of pattern you need.



C#
 private CancellationTokenSource cts;
//called from a 'start' button click
 private async void OnStartAsync(object arg)
        {
            IsStarted = true;

            cts = new CancellationTokenSource();
            CancellationToken token = cts.Token;
            try
            {
                //this runs your asynchronous method
                //you must check periodically to see 
                //if cancellation has been requested
                await Task.Run(() =>
                {
                    while (true)
                    {
                        Thread.Sleep(100);
                        //check to see if operation is cancelled
                        //and throw exception if it is
                        token.ThrowIfCancellationRequested();
                    }
                },token);
            }
            catch (OperationCanceledException)
            {
                IsStarted = false;

            }


        }

        private bool isStarted;
        public bool IsStarted
        {
            get
            {
                return isStarted;
            }
            set
            {
                isStarted = value;
                StartCommand.RaiseCanExecuteChanged();
                CancelCommand.RaiseCanExecuteChanged();

            }
        }

         //called from a 'cancel' button
        private void OnCancel(object arg)
        {
            cts.Cancel();
          
        }
 
Share this answer
 
You can do this with a CancellationTokenSource see example here: Parallel Programming: Task Cancellation | C# Frequently Asked Questions[^]
You will need this syntax though:
C#
Task.Factory.StartNew()
 
Share this answer
 
v2
Comments
Member 13801581 8-May-18 0:08am    
Unfortunately, it did not answer!

CancellationTokenSource cTokenSource = new CancellationTokenSource();
CancellationToken cToken = new CancellationToken();

private async void Search_Button_ClickAsync(object sender, EventArgs e)
{
cToken = cTokenSource.Token;
await (Task.Factory.StartNew(() =>
//...
,cToken));

private void CancelButton_ClickAsync(object sender, EventArgs e)
{
cTokenSource.Cancel();
}

}
RickZeeland 8-May-18 2:07am    
Then you might possibly need tokenSource.Token.ThrowIfCancellationRequested() in your task, especially if it has a tight loop.
[no name] 8-May-18 4:06am    
still didnot worked!!

CancellationTokenSource cTokenSource = new CancellationTokenSource();
CancellationToken cToken = new CancellationToken();

private async void Search_Button_ClickAsync(object sender, EventArgs e)
{
cToken = cTokenSource.Token;
await (Task.Factory.StartNew(() =>
{
cTokenSource.Token.ThrowIfCancellationRequested();
//...my program
}
,cToken));

private void CancelButton_ClickAsync(object sender, EventArgs e)
{
cTokenSource.Cancel();
}

}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900