-2

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 ?

Servy
  • 202,030
  • 26
  • 332
  • 449
IcanMakeIt
  • 59
  • 7
  • 1
    Why in the world are you starting a new thread only to *immediately* schedule work to be done in the UI thread? Don't do that. Also, why are you checking whether or not you're in the UI thread *when you just finished moving to a non-UI thread*; you know that you're in a non-UI thread, so there's no reason to check. – Servy Jan 08 '19 at 14:45
  • From post you linked, the author asked "What is the quickest way to create a nonblocking method call in C#". I would like to know what is the best practice for fire&forget jobs in background and why. – IcanMakeIt Jan 08 '19 at 14:47
  • @IcanMakeIt What makes you think those two things are different? – Servy Jan 08 '19 at 14:57
  • @Servy i have edited the code. I put to much things in the exampe, which I tested earlier. Thats why my question is here. I cannot see the differences. The pros and cons. There no differences ? – IcanMakeIt Jan 08 '19 at 15:02
  • And yes. Its duplicated. Bury it ): – IcanMakeIt Jan 09 '19 at 06:24

1 Answers1

-1

Looks to me like a straightforward case for a Task. Async doesn't seem appropriate as it assumes it's going to return and do magic things with context. Making the task async is just overkill, and BackgroundWorkers don't appear to be very different from Tasks.

In addition, you may wish to cancel your job at some point to close down gracefully, tasks support this easily, and you may wish to mark it as a long-running task; also a feature.

However! Your example suggests you're going to be messing with a rich text box, which is badly entangled with the foreground thread and the dispatcher. You may forget the task, but the app cannot, and the order of events arriving at the output will probably be important, and may not be the order in which you asked for them. Consider running a single task as a sort of service and sending messages, allowing the task to properly manage the process of adding lines to your output.

Richard Petheram
  • 805
  • 12
  • 16