How To Implement the Fire And Forget Method In C#

Scenario

There might be some scenario in your code, where some of the tasks should be performed in the background without affecting the current task. Today, I went into the same scenario, where I needed to fire the function, which consists of some function call, where the result of the function call is not used in the current function. I just needed to run it in the background, so there was no reason to wait for the function call result. We need to execute the next statement without waiting for the response, which can be easily achieved by Task factory.

Task Factory

Task.Factory.StartNew

  • It is a primary method for scheduling a new task.

    Please check here for more details about Task.Factory.StartNew

Example

Create one new console Application, using Visual Studio.

Open program.cs file and write the code, mentioned below, without using the Task.Factory.StartNew.

  1. class Program  
  2.     {  
  3.         static int a, b;  
  4.         public static void Add(int x,int y)  
  5.         {  
  6.             int z = x + y;  
  7.             Console.WriteLine("Add Result {0}", z);  
  8.          
  9.         }  
  10.         static void Main(string[] args)  
  11.         {  
  12.             Console.WriteLine("Enter first value for addition");  
  13.             a=Convert.ToInt32(Console.ReadLine());  
  14.             Console.WriteLine("Enter first value for addition");  
  15.             b=Convert.ToInt32(Console.ReadLine());  
  16.             Add(a,b);  
  17.             Console.WriteLine("I'm next to Add");  
  18.             Console.ReadKey();  
  19.   
  20.         }  
  21.     }  

From the code, mentioned above, you can observe that the static main function is calling the Add function, where the result of Add function is not used in the Main function, so without implementing the task factory, the statement next to add function call will be executed after the Add function completes its process.

Output
 
 

The code for Task.Factory.StartNew is shown below.
  1. class Program  
  2.     {  
  3.         static int a, b;  
  4.         public static void Add(int x,int y)  
  5.         {  
  6.             int z = x + y;  
  7.             Console.WriteLine("Add Result {0}", z);  
  8.         }  
  9.         static void Main(string[] args)  
  10.         {  
  11.             Console.WriteLine("Enter first value for addition");  
  12.             a=Convert.ToInt32(Console.ReadLine());  
  13.             Console.WriteLine("Enter first value for addition");  
  14.             b=Convert.ToInt32(Console.ReadLine());  
  15.             Task.Factory.StartNew(()=> Add(a,b));  
  16.             Console.WriteLine("I'm next to Add");  
  17.             Console.ReadKey();  
  18.   
  19.         }  
  20.     }  
The Task factory is implemented in the code, mentioned above. Now, the next state to add function call will be executed before the Add function completes its process.

Instead of Task.Factory.StartNew, we can use Task.Run function. 

Replace Task.Factory.StartNew to Task.Run, as shown in the code, mentioned below.

  1. class Program  
  2.     {  
  3.         static int a, b;  
  4.         public static void Add(int x,int y)  
  5.         {  
  6.             int z = x + y;  
  7.             Console.WriteLine("Add Result {0}", z);  
  8.               
  9.   
  10.         }  
  11.         static void Main(string[] args)  
  12.         {  
  13.             Console.WriteLine("Enter first value for addition");  
  14.             a=Convert.ToInt32(Console.ReadLine());  
  15.             Console.WriteLine("Enter first value for addition");  
  16.             b=Convert.ToInt32(Console.ReadLine());  
  17.             Task.Run(()=> Add(a,b));  
  18.             Console.WriteLine("I'm next to Add");  
  19.             Console.ReadKey();  
  20.   
  21.         }  
  22.     }  

The code, mentioned above will do the same functionality, which we got from StartNew function but based on the task usability, we can choose either Task.Run or Task.Factory.StartNew.

Output
 
 

Please check out the difference between Task.Run and Task.Factory.StartNew hereI hope, you enjoyed this blog. Your valuable feedback, question or comments about this blog are always welcome.
Ebook Download
View all
Learn
View all