1

I have a progressBar on my form and a button. When user clicks this button, the progressBar should be styled as "marquee" and the program begins to check is an URL is valid or not.. OK.

But when I click the button, the UI freezes until the HttpStatusCode returns true or false...

Here is the check code:

private bool RemoteFileExists(string url)
{
   try
   {
      HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
      request.Method = "HEAD";
      HttpWebResponse response = request.GetResponse() as HttpWebResponse;
      return (response.StatusCode == HttpStatusCode.OK);
   }
   catch
   {
      return false;
   }
}

And here is the button click code:

private async void button1_Click(object sender, EventArgs e)
{
   this.progressBar1.Style = System.Windows.Forms.ProgressBarStyle.Marquee;
   var result = RemoteFileExists("http://www.google.com/");
   if (Completed)
   {
      //ok
   }
   else
   {
      //not ok
   }
}
Marcelo Barganha
  • 369
  • 5
  • 14
  • You should use async methods: http://stackoverflow.com/questions/202481/how-to-use-httpwebrequest-net-asynchronously – Szer Jan 16 '15 at 04:12
  • Use `HttpWebRequest.BeginGetResponse()`. Look here: http://stackoverflow.com/questions/202481/how-to-use-httpwebrequest-net-asynchronously – Icemanind Jan 16 '15 at 04:13
  • http://msdn.microsoft.com/en-us/library/system.net.webrequest.getresponseasync(v=vs.110).aspx – Bharadwaj Jan 16 '15 at 04:15
  • `Exists()`-type methods are almost always bad design. It's usually better to just try and open/load a file, and handle the exception if (when!) it fails. – Joel Coehoorn Jan 16 '15 at 04:17

2 Answers2

1

The UI freezes because you are executing the RemoteFileExists method on the UI thread and receiving a response from a HttpWebRequest takes some time.
To solve this you have to execute RemoteFileExists in a different thread than the UI thread.
As your button1_Click method is already declared async the easiest way would be to declare RemoteFileExists as async too.
Then you can use the HttpWebRequest.GetResponseAsync method to asynchronously receive the response object.

private async Task<bool> RemoteFileExists(string url)
{
   try
   {
      HttpWebRequest request = WebRequest.CreateHttp(url);
      request.Method = "HEAD";

      using(var response = (HttpWebResponse) await request.GetResponseAsync())
      {
          return (response.StatusCode == HttpStatusCode.OK);
      }
   }
   catch
   {
      return false;
   }
}

Also when dealing with IDisposables you should take care of releasing all used resources by using the using statement or calling Dispose().
If you are using .NET Framework 4+ you can also use WebRequest.CreateHttp(string) to create your HttpWebRequest.

TorbenJ
  • 4,462
  • 11
  • 47
  • 84
0

Putting it simple just use this:

private void button1_Click(object sender, EventArgs e)
{
      this.progressBar1.Style = System.Windows.Forms.ProgressBarStyle.Marquee;
      Thread thread = new Thread(() => RemoteFileExists("http://www.google.com/"));
      thread.IsBackground = true;
      thread.SetApartmentState(ApartmentState.STA);
      thread.Start();


}

And do the check inside RemoteFileExists .

confusedMind
  • 2,573
  • 7
  • 33
  • 74