Ho appena visto 3 routine relative all'uso di TPL che fanno lo stesso lavoro; ecco il codice:
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();
}
Io proprio non capisco perché MS dà 3 modi diversi per eseguire i lavori in TPL, perché tutto il lavoro lo stesso: Task.Start()
, Task.Run()
e Task.Factory.StartNew()
.
Dimmi, sei Task.Start()
, Task.Run()
e Task.Factory.StartNew()
tutti utilizzati per lo stesso scopo o hanno un significato diverso?
Quando si dovrebbe usare Task.Start()
, quando si dovrebbe usare Task.Run()
e quando si dovrebbe usare Task.Factory.StartNew()
?
Ti prego, aiutami a capire il loro reale utilizzo come da scenario in dettaglio con esempi, grazie.
Task.Start
è effettivamente utile.
Task.Run
- forse questo risponderà alla tua domanda;)