How to cancel RunAutomaticTask manually

Hi,

  I use an asynchronous thread to manipulate a project using FileBasedProject.RunAutomaticTask calls. However, the PopulateProjectTranslationMemories task sometimes takes too long and I want the user to be able to cancel it with a button. 

  According to the GUI documentation, events assigned in RunAutomaticTask(Guid[], String, EventHandler<TaskStatusEventArgs>, EventHandler<TaskMessageEventArgs>) support cancellation. Does this mean that I should set the Cancel argument to true from within these events? If I understand correctly, they are triggered whenever there is a change in status (which may be a problem when progress stays on 95% for most of the time)? Or is there any way I can directly trigger the Cancel event for the Populate task?

Thanks for any clues,

Attila Grózli-Nagy

Parents
  • Hi ,

    The following is an example of how you can assign the cancel property to true from an automatic task that is executed via the project automation

    public class CancelBatchTaskExample
    	{
    		private CancellationTokenSource _cancellationToken;
    
    		public void Execute()
    		{
    			// 1. Get the projects controller
    			var projectsController = SdlTradosStudio.Application.GetController<ProjectsController>();
    
    			// 2. Get the current project (if null, get first selected project)
    			var currentProject = projectsController.CurrentProject ?? projectsController.SelectedProjects.FirstOrDefault();
    			if (currentProject == null)
    			{
    				return;
    			}
    
    			// 3. Get the target language file GUIDs
    			var language = currentProject.GetProjectInfo().TargetLanguages.FirstOrDefault();
    			var projectFiles = currentProject.GetTargetLanguageFiles(language);
    			var guids = projectFiles.Select(projectFile => projectFile.Id).ToArray();
    
    			// 4. create new cancellation token; cancel the previous, if exists
    			_cancellationToken?.Cancel();
    			_cancellationToken = new CancellationTokenSource();
    
    			// 5. Run a task
    			System.Threading.Tasks.Task.Run(delegate
    			{
    				// demonstration of cancelling the task after 1 second
    				_cancellationToken.CancelAfter(TimeSpan.FromSeconds(1));
    
    				return currentProject.RunAutomaticTask(guids, AutomaticTaskTemplateIds.AnalyzeFiles, StatusEventHandler, MessageEventHandler);
    
    			}).ContinueWith(t =>
    			{
    				Trace.WriteLine(t.Result.Status);
    				foreach (var message in t.Result.Messages)
    				{
    					Trace.WriteLine(message.Message);
    				}
    			});
    		}
    
    		private void MessageEventHandler(object sender, TaskMessageEventArgs e)
    		{
    			if (_cancellationToken.IsCancellationRequested)
    			{
    				e.Cancel = true;
    			}
    		}
    
    		private void StatusEventHandler(object sender, TaskStatusEventArgs e)
    		{
    			
    			if (_cancellationToken.IsCancellationRequested)
    			{
    				e.Cancel = true;
    			}
    		}
    	}

Reply
  • Hi ,

    The following is an example of how you can assign the cancel property to true from an automatic task that is executed via the project automation

    public class CancelBatchTaskExample
    	{
    		private CancellationTokenSource _cancellationToken;
    
    		public void Execute()
    		{
    			// 1. Get the projects controller
    			var projectsController = SdlTradosStudio.Application.GetController<ProjectsController>();
    
    			// 2. Get the current project (if null, get first selected project)
    			var currentProject = projectsController.CurrentProject ?? projectsController.SelectedProjects.FirstOrDefault();
    			if (currentProject == null)
    			{
    				return;
    			}
    
    			// 3. Get the target language file GUIDs
    			var language = currentProject.GetProjectInfo().TargetLanguages.FirstOrDefault();
    			var projectFiles = currentProject.GetTargetLanguageFiles(language);
    			var guids = projectFiles.Select(projectFile => projectFile.Id).ToArray();
    
    			// 4. create new cancellation token; cancel the previous, if exists
    			_cancellationToken?.Cancel();
    			_cancellationToken = new CancellationTokenSource();
    
    			// 5. Run a task
    			System.Threading.Tasks.Task.Run(delegate
    			{
    				// demonstration of cancelling the task after 1 second
    				_cancellationToken.CancelAfter(TimeSpan.FromSeconds(1));
    
    				return currentProject.RunAutomaticTask(guids, AutomaticTaskTemplateIds.AnalyzeFiles, StatusEventHandler, MessageEventHandler);
    
    			}).ContinueWith(t =>
    			{
    				Trace.WriteLine(t.Result.Status);
    				foreach (var message in t.Result.Messages)
    				{
    					Trace.WriteLine(message.Message);
    				}
    			});
    		}
    
    		private void MessageEventHandler(object sender, TaskMessageEventArgs e)
    		{
    			if (_cancellationToken.IsCancellationRequested)
    			{
    				e.Cancel = true;
    			}
    		}
    
    		private void StatusEventHandler(object sender, TaskStatusEventArgs e)
    		{
    			
    			if (_cancellationToken.IsCancellationRequested)
    			{
    				e.Cancel = true;
    			}
    		}
    	}

Children
No Data