Simplest way to do a fire and forget method in C#?

Viewed 110548

I saw in WCF they have the [OperationContract(IsOneWay = true)] attribute. But WCF seems kind of slow and heavy just to do create a nonblocking function. Ideally there would be something like static void nonblocking MethodFoo(){}, but I don't think that exists.

What is the quickest way to create a nonblocking method call in C#?

E.g.

class Foo
{
    static void Main()
    {
        FireAway(); //No callback, just go away
        Console.WriteLine("Happens immediately");
    }

    static void FireAway()
    {
        System.Threading.Thread.Sleep(5000);
        Console.WriteLine("5 seconds later");
    }
}

NB: Everyone reading this should think about if they actually want the method to finish. (See #2 top answer) If the method has to finish, then in some places, like an ASP.NET application, you will need to do something to block and keep the thread alive. Otherwise, this could lead to "fire-forget-but-never-actually-execute", in which case,of course, it would be simpler to write no code at all. (A good description of how this works in ASP.NET)

11 Answers
ThreadPool.QueueUserWorkItem(o => FireAway());

(five years later...)

Task.Run(() => FireAway());

as pointed out by luisperezphd.

To add to Will's answer, if this is a console application, just throw in an AutoResetEvent and a WaitHandle to prevent it exiting before the worker thread completes:

Using System;
Using System.Threading;

class Foo
{
    static AutoResetEvent autoEvent = new AutoResetEvent(false);

    static void Main()
    {
        ThreadPoolQueueUserWorkItem(new WaitCallback(FireAway), autoEvent);
        autoEvent.WaitOne(); // Will wait for thread to complete
    }

    static void FireAway(object stateInfo)
    {
        System.Threading.Thread.Sleep(5000);
        Console.WriteLine("5 seconds later");
        ((AutoResetEvent)stateInfo).Set();
    }
}

An easy way is to create and start a thread with parameterless lambda:

(new Thread(() => { 
    FireAway(); 
    MessageBox.Show("FireAway Finished!"); 
}) { 
    Name = "Long Running Work Thread (FireAway Call)",
    Priority = ThreadPriority.BelowNormal 
}).Start();

By using this method over ThreadPool.QueueUserWorkItem you can name your new thread to make it easier for debugging. Also, don't forget to use extensive error handling in your routine because any unhandled exceptions outside of a debugger will abruptly crash your application:

enter image description here

Calling beginInvoke and not catching EndInvoke is not a good approach. Answer is simple: The reason that you should call EndInvoke is because the results of the invocation (even if there is no return value) must be cached by .NET until EndInvoke is called. For example if the invoked code throws an exception then the exception is cached in the invocation data. Until you call EndInvoke it remains in memory. After you call EndInvoke the memory can be released. For this particular case it is possible the memory will remain until the process shuts down because the data is maintained internally by the invocation code. I guess the GC might eventually collect it but I don't know how the GC would know that you have abandoned the data vs. just taking a really long time to retrieve it. I doubt it does. Hence a memory leak can occur.

More can be found on http://haacked.com/archive/2009/01/09/asynchronous-fire-and-forget-with-lambdas.aspx

Almost 10 years later:

Task.Run(FireAway);

I would add exception handling and logging inside FireAway

The simplest way to do fire-and-forget is to use the discard pattern:

_ = MyFireAndForgetTask(myParameters);

This notifies your method that the result of your Task will not be needed and execution of the thread is not stalled.

Please note that the Task must call Task.Run within it to be asynchronous using this pattern. Using our previous method as an example:

Task MyFireAndForgetTask(myParameters)
{
    return Task.Run(/* put Task, Func<T>, or Action here*/);
}

If this step is ignored, the Task will run synchronously and will not behave as expected.

Furthermore the assignment pattern can be used. This is useful for when the method runs until the last line but hangs until the Task is finished. We will utilize Task.Wait() for this. Using our previous method as an example:

void MyCustomEventHandler(object sender, EventArgs args)
{
    /* perform some code here */
    var myTask = MyFireAndForgetTask(myParameters);
    /* perform some more code here; thread is not blocked */

    /// Hang the method until the Task is completed.
    /// using "await myTask;" is equivalent.
    myTask.Wait();
}

This will perform a fire-and-forget-till-completion, which is mandatory on some platforms (i.e. ASP.NET).

If you want to test in Console keep in mind that Console.ReadKey() or something like that is needed before Console loses its thread by Press any key to continue ...

public static void Main()
{
    Task.Factory.StartNew(async () => 
    {
         await LongTaskAsync();         
    }, TaskCreationOptions.LongRunning).ConfigureAwait(false);
    
    Console.WriteLine("Starts immediately");
    Console.ReadKey();
}

static async Task LongTaskAsync()
{
    await Task.Delay(5000);
    Console.WriteLine("After 5 seconds delay");
}
Related