For representing any work that can be done with the help of an object. This object is called a task. In another word, it is an object representing any word that must be completed. When we want to run something in parallel, we use tasks. It is opposite to thread in C# programming and will return a result. It also gives an option of “cancellation”.
Properties of Task in C# Programming
“CurrentId” is used for giving the ID of the task which is currently running.
“Status” is used for returning the Status of Task.
“IsCanceled” is used to check whether the task is canceled or not and returns a value that indicates it.
“IsCompleted” is used to check whether the task is completed or not and returns a value that indicates it.
“IsFaulted” is used to check whether the task paused because of an unhandled exception or not and returns a value that indicates it.
The “factory” is used for returning the exceptions due to which the Task terminates before time.
Methods of Task in C# Programming
The method “start()” is used for the task to start.
The keyword “ConfigureAwait()” is used to Await the task.
The “ContinueWith()” keyword is used for the task continuation.
The keyword “Run()” is used to run the task.
The “RunSynchronously()” will synchronously run the task.
We use “Delay()” in which the task is created so that it is completed after a fixed number of milliseconds.
We use “Wait()” so that the task is to Wait for complete execution.
The “WaitAny()” keyword is used for the specific task to complete.
We use the “Dispose()” method to release the resources.
Example # 1: To Create and Start a Task in C# Programming in Ubuntu 20.04
In this instance, we will show you how to generate a new task and start the task that is created. We are going to create and start the task in C# programming in Ubuntu 20.04. Write this given code in Ubuntu text editor and save the file with the filename which you want having an extension of “.cs”.
In this given example, we have “using System” in C# programming which we use for getting the functions and methods that are required in the program. We include this “using System” in the first line of code. This is compulsory in C# programming. Now, we import “using System.Threading.Tasks” which is the namespace in our program. By using this namespace, we can create the task and also get access to the objects of the task with the help of the task class.
Now, we created a namespace with the name “TaskProgram”. We defined a class named “CreateTask”. Inside this class, we invoked a “main” function which is “static void main(string args)”. This “main” function is kept static and the “(string args)” is used for the arguments’ representation of this C# program.
Inside this “main” function we have “Console.WriteLine” which prints the line on the screen. As in this code, we have “Console.WriteLine(Our Main Thread is created)”, which will print the same line on the terminal of Ubuntu 20.04, when we execute that program. Now, we are creating a Task with the task object named “tsk1”. We pass the parameter “PrintInfo” to this new Task. In the next line, “Task.Start()” is there which is used to start the task which we created in the above line of code.
After this, we have “Console.ReadLine()”, here it is blocking call. We create the PrintInfo() function which is static and use a “for loop” in this “static void PrintInfo()”. The “for(int a=1, a<=4, a++)” means we declare and initialize an integer “a” and assign “1” to “a”. After this, we have a condition which is “a<=4”, which means that the loop is executed until the value of “a” is less than or equal to “4”. Whenever the condition is true, it will print the given line below this “for loop” with the help of “Console.WriteLine”. The loop will increment the value of “a” each time it executes. And when the given condition is not satisfied, then it comes out and prints that line which is given outside the curly braces.
Now for the output, we use commands in Ubuntu 20.04. All these commands are given below in this image. You must run these same commands on the Ubuntu terminal but remember one thing with the “mcs” command you use the extension of “.cs” with the filename and with the “mono” command use the file extension “.exe” with the filename. We are going to show you the output of the above code in the image which is given below.
In this output, we see that it first prints the line and after this, it creates a task and for loop will print the values. After that when the condition is false it will print the last line.
Example # 2: Using the “Wait()” Method of Task in Ubuntu 20.04
In this example, we use the “Wait()” method of the task so that the task has to wait for the other task to complete. Now, have a look at this example. Run this code on your Ubuntu text editor and check the output.
In the above code, we have “using System” and “using System.threading.Task”. The “using System” is used for accessing functions and methods of the C# program and the “using System.threading.Task ” is the namespace that is used for creating tasks and accessing the task objects as we discussed in the previous example in detail. Now, we have a namespace with the name “CSharpTask”. After this namespace, we created a class named “WaitTask”.
After this, we have a “main” function which we describe in the above example. We are going to create a task inside the “main” function. In this C# programming, we have the “Task” class which is used for creating a task. Here, we create a task as “t1” and the “Run()” method is used to run the task that is created. Inside this, we call a “PrintInfo()” function which is described below in this code. After the completion of the task, the lines below this will print by using “Console.WriteLine()”.
After this, we describe that function which we called above “PrintInfo()” inside this “PrintInfo” we have the “for loop” same as we have in the above code and the working of this “for loop” is the same as above code. But here, we see that the task has to wait for the completion of the task because we use the “Wait()” method.
We get the output with the same method as we discussed in the code above. Just the filename is changed here.
Here, we see in the output that the first task waits until the task is completed and when the task is completed, it prints the lines when we write in the code.
In this guide, we have explained how to create a task, and how to run and start that task, and how the task waits for the task completion in C# programming in Ubuntu 20.04. I hope it will help to understand the concept of the task in C# programming and you learn a lot from this article. We have a lot of approaches that we used in the tasks in C# programming. Here, we discussed a few methods in this guide but in detail, we provide detailed information about the C# Task program with a practical demonstration of different examples. By understanding these basic notions, you will be able to perform this on complex programming in the future.