in Technology by
What is Parallel Task in C#?

1 Answer

0 votes
by
.Net 4.0 added a new feature of Parallel Programming. This Parallel Programming can be done using Task Parallel Library (TPL). This library is available from the namespace System.Threading.Tasks.
 
The Task Parallel Library (TPL) is the base of the Task-based programming. It consists of:
  • Task class is used to do the unit of work you will code for instead of the previous thread model.
  • Parallel class is a static class that exposes a task-based version of some parallel-nature problems. 
If the same work is to be done with more than 1 worker then what do I need to write?
  1. Stopwatch watch = new Stopwatch();  
  2. watch.Start();  
  3. System.Threading.Tasks.Parallel.For(0, 15, index =>  
  4. {  
  5.     Console.WriteLine("Digging completed for " + index + "mts. area ");  
  6.     Thread.Sleep(1000);                   
  7. }  
  8. );  
  9. watch.Stop();  
  10. Console.WriteLine("Time required time for Parallel digging: " + watch.Elapsed.Seconds); 
Output
 
Result
 
As you can see the difference in timing required by a worker to complete the task single-handedly and by more than 1 respectively. This gives a huge improvement in execution timing when Parallel. For is used.
 
Another alternative is by using Parallel. ForEach that is similar to a foreach loop.

Related questions

0 votes
    i want to run a task every 5 minutes. i've tried to solve it with an IntentService and ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 26, 2022 in Education by JackTerrance
0 votes
    What are the difference between Task and Thread in .NET?...
asked Feb 16, 2023 in Technology by JackTerrance
0 votes
    Which plugin helps in task dependencies? A. No gulp plug-ins needed B. gulp-order C. gulp-seq C. gulp-depend...
asked Feb 3, 2023 in Technology by JackTerrance
0 votes
    Which command in the CLI will trigger the 'default` task? A. run default gulp B. run gulp default C. gulp D. All the options E. run gulp...
asked Feb 3, 2023 in Technology by JackTerrance
0 votes
    The default task representation in gulp will be like _________. A. gulp.call('default',function(){ 'default'}); B. gulp.task(' ... ; D. gulp.call('default',function(){ 'd',null});...
asked Jan 31, 2023 in Technology by JackTerrance
0 votes
    Which of the below does the task of binding the parameters passed to the HTTP method to HTTP Header while using ... ? 1. @PathParam 2. @QueryParam 3. @HeaderParam 4. @PathVariable...
asked Jun 24, 2021 in Technology by JackTerrance
0 votes
    Write the Port Numbers for NameNode, Task Tracker and Job Tracker?...
asked Jun 1, 2021 in Technology by JackTerrance
0 votes
    __________ is achieved by distributing privileges for accomplishing a task to different people. (1)Principle of Least Privilege (2)Privilege Escalation (3)Separation of duties...
asked May 31, 2021 in Technology by JackTerrance
0 votes
    The PEAS in the task environment is about____________. Peer, Environment, Actuators, Sense Performance, Environment, ... , Environment, Actuators, Sensors None of the above...
asked Mar 8, 2021 in Technology by JackTerrance
0 votes
    What is meant by the command task in Azure?...
asked Dec 28, 2020 in Technology by JackTerrance
0 votes
    The default task representation in gulp will be like _________. a) gulp.call('default',function() ... 🔗Source: Continuous Integration Interview Questions and Answers...
asked Dec 20, 2020 in Technology by Editorial Staff
0 votes
    What Is The Difference Between A Verilog Task And A Verilog Function?...
asked Dec 12, 2020 in Technology by JackTerrance
0 votes
0 votes
    You have been assigned the task of reshaping the data wherein you have to convert the wide format data into long format data and vice versa. How will you carry out this operation?...
asked Oct 16, 2020 in Technology by JackTerrance
0 votes
    What are the different ways to implement parallel processing in Informatica?...
asked Mar 27, 2021 in Technology by JackTerrance
...