Mar 7, 2017 05:54 AM
(This post was last modified: Mar 7, 2017 05:56 AM by Y3LI0NZ.)
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.
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.
