Hakan Axheim

Hakan Axheim

  • NA
  • 201
  • 32.5k

Aborting a Service safe

Feb 26 2021 7:44 AM
Hi,
 
I have a service that do some work with a polling intervall.
Task task = new Task( delegate { DoWork(); });
task.Wait(Convert.ToInt32(ConfigurationManager.AppSettings["PollingInterval"]));
task.RunSynchronously();
 
My question is how to abort the Task safe?
 
Is it enough with
 
try
   Task task = new Task( delegate { DoWork(); });
   task.Wait(Convert.ToInt32(ConfigurationManager.AppSettings["PollingInterval"]));
   task.RunSynchronously();
}
catch (ThreadAbortException threadAbortException)
{
   // Clean up 
 

Answers (2)