Here is an example of retrieving a string response asynchronously using callbacks,
This file contains 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
using System; | |
using System.Net.Http; | |
using System.Threading.Tasks; | |
namespace async_with_callbacks | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var httpClient = new HttpClient(); | |
httpClient.GetStringAsync("http://google.com").ContinueWith(getStringTask => | |
{ | |
Console.WriteLine($"Google response: {getStringTask.Result}"); | |
}).Wait(); | |
Console.ReadLine(); | |
} | |
} | |
} |
This file contains 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
using System; | |
using System.Net.Http; | |
using System.Threading.Tasks; | |
namespace async_with_callbacks | |
{ | |
class Program | |
{ | |
static async Task Main(string[] args) | |
{ | |
var httpClient = new HttpClient(); | |
var getStringTaskResult = await httpClient.GetStringAsync("http://google.com"); | |
Console.WriteLine($"Google response: {getStringTaskResult}"); | |
Console.ReadLine(); | |
} | |
} | |
} |
This file contains 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
using System; | |
using System.Net.Http; | |
using System.Threading.Tasks; | |
namespace async_with_multiple_callbacks | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var httpClient = new HttpClient(); | |
httpClient.GetStringAsync("http://google.com").ContinueWith(getStringTaskGoogle => | |
{ | |
httpClient.GetStringAsync("http://yahoo.com").ContinueWith(getStringTaskYahoo => | |
{ | |
httpClient.GetStringAsync("http://bing.com").ContinueWith(getStringTaskBing => | |
{ | |
Console.WriteLine($"Google response: {getStringTaskGoogle.Result}"); | |
Console.WriteLine($"Yahoo response: {getStringTaskYahoo.Result}"); | |
Console.WriteLine($"Bing response: {getStringTaskBing.Result}"); | |
}, TaskContinuationOptions.AttachedToParent); | |
}, TaskContinuationOptions.AttachedToParent); | |
}).Wait(); | |
Console.ReadLine(); | |
} | |
} | |
} |
This file contains 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
using System; | |
using System.Net.Http; | |
using System.Threading.Tasks; | |
namespace async_with_multiple_awaits | |
{ | |
class Program | |
{ | |
static async Task Main(string[] args) | |
{ | |
var httpClient = new HttpClient(); | |
var getStringTaskGoogleResult = await httpClient.GetStringAsync("http://google.com"); | |
var getStringTaskYahooResult = await httpClient.GetStringAsync("http://yahoo.com"); | |
var getStringTaskBingResult = await httpClient.GetStringAsync("http://bing.com"); | |
Console.WriteLine($"Google response: {getStringTaskGoogleResult}"); | |
Console.WriteLine($"Yahoo response: {getStringTaskYahooResult}"); | |
Console.WriteLine($"Bing response: {getStringTaskBingResult}"); | |
Console.ReadLine(); | |
} | |
} | |
} |