![]() |
|
[C#] A tip when making web Requests - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [C#] A tip when making web Requests (/showthread.php?tid=5568686) |
A tip when making web Requests - Y3LI0NZ - Mar 7, 2017 Sorry this doesn't have actual screeshots of it, my pc has some bug and is being weird. I wrote this a while back under another username, thought it mignt be helpful on here for all the noobies out there. Often times when I use programs that require WebRequests, they block the calling thread. Simple terms, the user interface freezes until it's done. There are a few remedies to fix this but you might still run into UI freezing. Create a new thread: Code: new Thread(new ThreadStart(() => { using (WebClient wc = new WebClient()) { string webString = wc.DownloadString("http://www.google.com"); Console.WriteLine(webString); } })).Start(); Or WebClient offers a method to Download Asynchronously - https://msdn.microsoft.com/en-us/library...10%29.aspx Code: using (WebClient wc = new WebClient()) { wc.DownloadStringCompleted += (sender, e) => { Console.WriteLine(e.Result); }; wc.DownloadStringAsync(new Uri("http://google.com")); } Now, there maybe a time where you go to put the web request in a new thread or use DownloadStringAsync() and it still blocks the UI. Why? The simple fix is adding this line to your code: Code: WebRequest.DefaultProxy = null; Why does this work? Because before the request is even sent, It will check Internet Explorers proxy setting by default which may cause in a delay/ui freeze even though you took precautions. So setting it the null ahead of time skips that process and solves problems. * - maldev0 - Mar 7, 2017 wrong section you should post this in "C# Programming Language" * - Ninja - Mar 7, 2017 Use code tags next time, please. |