M
Markus Freitag
Guest
Hello!
Can someone please explain to me visually (in graphic) what the differences are.
When should I use the call?
Task.Run(()=>
When should I use the other call?
Task.Factory.StartNew(async ()
Does this have to do with the Factory Pattern?
If so, does anyone know a good understandable example?
Thank you very much for the explanation in advance.
Greetings Markus
Task<Person> taskOne = Task.Factory.StartNew(async () =>
{
public Task<int> SlowWork()
{
return Task.Run(()=>{……});
}
public async Task CallSlowTask()
{
// 1. Call the async code, let it run for a while in another thread
// (Task) in the async mode. The work is usually very slow
var runningTask = SlowWork();
// 2. Here we can run the sync code, it will block the UI or main
// thread, so please make them as simple and fast as possible.
……
// 3. After this, when we wanna get the result from SlowWork(),
// we can use await to force that we want to fetch the result.
// Because "SlowWork()" has been running for a while, maybe the
// result comes in. But if the result doesn't come in yet, it
// will wait in the async mode (NOT blocking the current main thread)
var result = await runningTask;
}
Continue reading...
Can someone please explain to me visually (in graphic) what the differences are.
When should I use the call?
Task.Run(()=>
When should I use the other call?
Task.Factory.StartNew(async ()
Does this have to do with the Factory Pattern?
If so, does anyone know a good understandable example?
Thank you very much for the explanation in advance.
Greetings Markus
Task<Person> taskOne = Task.Factory.StartNew(async () =>
{
public Task<int> SlowWork()
{
return Task.Run(()=>{……});
}
public async Task CallSlowTask()
{
// 1. Call the async code, let it run for a while in another thread
// (Task) in the async mode. The work is usually very slow
var runningTask = SlowWork();
// 2. Here we can run the sync code, it will block the UI or main
// thread, so please make them as simple and fast as possible.
……
// 3. After this, when we wanna get the result from SlowWork(),
// we can use await to force that we want to fetch the result.
// Because "SlowWork()" has been running for a while, maybe the
// result comes in. But if the result doesn't come in yet, it
// will wait in the async mode (NOT blocking the current main thread)
var result = await runningTask;
}
Continue reading...