![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Tasks in C#
Task represents an asynchronous operation in C#. The following states how you can start a task in C#.
Use a delegate to start a task.
Task t = new Task(delegate { PrintMessage(); }); t.Start();
Use Task Factory to start a task.
Task.Factory.StartNew(() => {Console.WriteLine("Welcome!"); });
You can also use Lambda.
Task t = new Task( () => PrintMessage() ); t.Start();
The most basic way to start a task is using the run().
Example
using System; using System.Threading.Tasks; public class Example { public static void Main() { Task task = Task.Run( () => { int a = 0; for (a = 0; a <= 1000; a++){} Console.WriteLine("{0} loop iterations ends",a); } ); task.Wait(); } }
Advertisements