Run Code
|
API
|
Code Wall
|
Users
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Get a webpage - the async way
//Title of this code //Rextester.Program.Main is the entry point for your code. Don't change it. using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; using System.Net.Http; using System.Threading.Tasks; using System.Threading; namespace Rextester { public class Program { public static async void Main(string[] args) { ThreadPool.QueueUserWorkItem(f => RequestData("http://google.com")); Thread.Sleep(2000); } static async void RequestData(string uri) { using (var client = new HttpClient()) { Console.WriteLine(System.Threading.Thread.CurrentThread.ManagedThreadId); Console.WriteLine(await client.GetStringAsync(uri)); //how's that differs from sync code? The thread is not being used while IO occurs. Console.WriteLine(System.Threading.Thread.CurrentThread.ManagedThreadId); //note - different thread, that was started only when IO finished. } } } }
run
|
edit
|
history
|
help
0
Please
log in
to post a comment.
Hello World Program
Date example
function in event
My Wall
Decrypt Encrypt
uniqueness 2.8
Remove BOLD formatting from html
gd
memes
CommandForce3
Please log in to post a comment.