This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface MyInterface | |
{ | |
Task<int> GetMagicNumber(); | |
} | |
public class MyClass : MyInterface | |
{ | |
public Task<int> GetMagicNumber() | |
{ | |
return Task.FromResult(3); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private async Task<string> DoWorkAsync() | |
{ | |
HttpClient client = new HttpClient(); | |
string result1 = await client.GetStringAsync(url1); | |
// ... | |
string result2 = await client.GetStringAsync(url2); | |
// ... | |
string result3 = await client.GetStringAsync(url3); | |
// ... | |
return $"Results: {result1}{result2}{result3}"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private async Task<string> DoWorkAsync() | |
{ | |
HttpClient client = new HttpClient(); | |
string result = await client.GetStringAsync(url); | |
return $"Result: {result}"; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private async Task DoWorkAsync() | |
{ | |
// Comienza a ejecutarse como un método síncrono | |
HttpClient client = new HttpClient(); | |
// await [Task]: se inicia la operación asíncrona y se suspende | |
// la ejecución del resto del código | |
await client.GetStringAsync(url); | |
// resto del código | |
// ... se ejecutará cuando la tarea asíncrona se complete | |
// ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HomeController: Controller | |
{ | |
public IActionResult Get() | |
{ | |
var result = DoWork(100); | |
return View(result); | |
} | |
public long DoWork(int n) | |
{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Este método está vinculado a la CPU. Si se usa desde una | |
/// UI utilizar Task.Run para no bloquear la interfaz | |
/// <summary> | |
void DoWork() | |
{ | |
//... CPU-Bound work | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public long DoWork(int n) | |
{ | |
// CPU-Bound work | |
// ... | |
return fibonacciNumber; | |
} | |
public Task<long> DoWorkAsync(n) | |
{ | |
return Task.Run(() => DoWork(n)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HomeController: Controller | |
{ | |
public IActionResult GetAsync() | |
{ | |
var result = DoWork(100); | |
return View(result); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HomeController: Controller | |
{ | |
public async Task<IActionResult> GetAsync() | |
{ | |
var result = await DoWork(100); | |
return View(result); | |
} | |
public Task<long> DoWork(int n) | |
{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public long DoWork(int n) | |
{ | |
// CPU-Bound work | |
// ... | |
return fibonacciNumber; | |
} |
NewerOlder