Skip to content

Instantly share code, notes, and snippets.

@ierhalim
Last active December 26, 2020 23:25
Show Gist options
  • Save ierhalim/f1223a735a4b9c549e510e2248391a51 to your computer and use it in GitHub Desktop.
Save ierhalim/f1223a735a4b9c549e510e2248391a51 to your computer and use it in GitHub Desktop.
Y1O1 C# Paralel programlama
using System;
using System.Threading.Tasks;
namespace CreatingAndStartingTask
{
class Program
{
static void DoWork(string message)
{
for (int i = 0; i < 1000; i++)
{
Console.Write(message);
}
}
static void Main(string[] args)
{
// Yeni bir task instance ı oluşturup. Action parametresine gönderdiğimiz DoWork methodunu anında çalıştırır.
// StartNew methodu aslında iki iş yapar hem bir Task instance ı oluşturup geri döndürür hemde oluşturduğu instance üzerindeki Start methodunu çalıştırır.
Task.Factory.StartNew(() =>
{
DoWork("T1");
Console.WriteLine("DoWork Completed for T1");
});
// Instance ı kendimiz oluşturduğumuzda, action parametresindeki methodu çalıştırmaz.
// Oluşturduğumuz instancetaki Start methodunu tetiklediğimizde DoWork çalışacaktır.
var task = new Task(() =>
{
DoWork("T2");
Console.WriteLine("DoWork Completed for T2");
});
Console.WriteLine("Main Thread Log 1");
// Task constructor' ı içerisinde tanımladığımız alttaki satırdan sonra çalışacaktır.
task.Start();
Console.WriteLine("Main Thread Log 2");
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment