اصول برنامه نویسی موازی درNET. نسخه 4 بخش اول - 1
نویسنده: احمد مقدس خو
تاریخ: ۱۳۹۱/۰۳/۳۱ ۱۲:۷
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
using System;
using System.Threading.Tasks;
namespace Listing_01 {
class Listing_01 {
static void Main(string[] args) {
Task.Factory.StartNew(() => {
Console.WriteLine("Hello World");
});
// wait for input before exiting
Console.WriteLine("Main method complete. Press enter to finish.");
Console.ReadLine();
}
}
using System.Threading.Tasks;
using System.Threading;
Task.Factory.StartNew(() => {
Console.WriteLine("Hello World");
});
Main method complete. Press enter to finish. Hello World
Task task1 = new Task(new Action(printMessage));
Task task2 = new Task(delegate {
printMessage();
});
Task task3 = new Task(() => printMessage());
Task task4 = new Task(() => {
printMessage();
});
using System;
using System.Threading.Tasks;
namespace Listing_02 {
class Listing_02 {
static void Main(string[] args) {
// use an Action delegate and a named method
Task task1 = new Task(new Action(printMessage));
// use a anonymous delegate
Task task2 = new Task(delegate {
printMessage();
});
// use a lambda expression and a named method
Task task3 = new Task(() => printMessage());
// use a lambda expression and an anonymous method
Task task4 = new Task(() => {
printMessage();
});
task1.Start();
task2.Start();
task3.Start();
task4.Start();
// wait for input before exiting
Console.WriteLine("Main method complete. Press enter to finish.");
Console.ReadLine();
}
static void printMessage() {
Console.WriteLine("Hello World");
}
}
}
Main method complete. Press enter to finish. Hello World Hello World Hello World Hello World
Task.Factory.StartNew(() =>
{
Thread.Sleep(1000);
}, TaskCreationOptions.LongRunning);