Właśnie zobaczyłem 3 procedury dotyczące korzystania z TPL, które wykonują tę samą pracę; oto kod:
public static void Main()
{
Thread.CurrentThread.Name = "Main";
// Create a task and supply a user delegate by using a lambda expression.
Task taskA = new Task( () => Console.WriteLine("Hello from taskA."));
// Start the task.
taskA.Start();
// Output a message from the calling thread.
Console.WriteLine("Hello from thread '{0}'.",
Thread.CurrentThread.Name);
taskA.Wait();
}
public static void Main()
{
Thread.CurrentThread.Name = "Main";
// Define and run the task.
Task taskA = Task.Run( () => Console.WriteLine("Hello from taskA."));
// Output a message from the calling thread.
Console.WriteLine("Hello from thread '{0}'.",
Thread.CurrentThread.Name);
taskA.Wait();
}
public static void Main()
{
Thread.CurrentThread.Name = "Main";
// Better: Create and start the task in one operation.
Task taskA = Task.Factory.StartNew(() => Console.WriteLine("Hello from taskA."));
// Output a message from the calling thread.
Console.WriteLine("Hello from thread '{0}'.",
Thread.CurrentThread.Name);
taskA.Wait();
}
Ja po prostu nie rozumiem, dlaczego MS daje 3 różne sposoby uruchamiania zadań w OC, ponieważ wszystkie działają tak samo: Task.Start()
, Task.Run()
i Task.Factory.StartNew()
.
Powiedz mi, są Task.Start()
, Task.Run()
i Task.Factory.StartNew()
używane w tym samym celu lub mają one inne znaczenie?
Kiedy używać Task.Start()
, kiedy używać, Task.Run()
a kiedy używać Task.Factory.StartNew()
?
Proszę, pomóż mi szczegółowo zrozumieć ich rzeczywiste użycie zgodnie ze scenariuszem wraz z przykładami.
Task.Run
- może to odpowie na twoje pytanie;)