I need to run class method in background. This method will be fire&forget with no return value. It can be run multiple times with different parameters at same time. I don't won't this background job, to interfere with main app.
Should it be done as new thread ? as asynchronous ? I am confused here. Below some code snippet.
private void timerAction_Tick(object sender, EventArgs e)
{
Action actionTask = () => new StuffService().FireAndForgetCommand();
actionTask.BeginInvoke(actionTask.EndInvoke, null);
Task.Factory.StartNew(() => new StuffService().FireAndForgetCommand());
Task.Factory.StartNew(() => new StuffService().FireAndForgetCommand(), TaskCreationOptions.RunContinuationsAsynchronously);
}
public class StuffService
{
public void FireAndForgetCommand()
{
// do some stuff, longer then 100ms
// if exception occurs then log it
for(int i = 0; i < Int32.MaxValue; i++)
{ }
}
}
I am trying to understand whats the best (most save, most efficient ...) way to achieve this.
Or maybe should I just use BackgroundWorker Class ?