Note: I'm only gonna be showing WebClient() examples here, considering it's higher leveled built right on top of httpwebrequest. Furthermore, it's easier.
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:
Or WebClient offers a method to Download Asynchronously - https://msdn.microsoft.com/en-us/library...10%29.aspx
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:
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.
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.
This account is currently banned
Ban reason: Learn from your mistakes.
Ban reason: Learn from your mistakes.