0

I made a httpwebrequest in a sub. The form on my program has a start button which calls the sub. I want to add a Stop button to it to stop the httwebrequest if the user so chooses.

How would I do this?

Yahia
  • 69,653
  • 9
  • 115
  • 144
user1017524
  • 241
  • 9
  • 17
  • 2
    Since you've tagged this with `C#`, I assume by "sub" you actually mean "function" or "method"? C# does not use any of those keywords, unlike VB.NET, which uses `sub` and `function`, but they are generally all called `functions` or `methods` in C# – Andrew Barber Feb 05 '12 at 16:20

2 Answers2

5

Call Abort on the HttpWebRequest.

BEWARE that this might cause some strange behaviour... for example: depending on how you made the request and in what state it is this might lead to a deadlock.

Yahia
  • 69,653
  • 9
  • 115
  • 144
  • How would I call it? Using an if statement saying if the button was clicked to abort? – user1017524 Feb 05 '12 at 16:20
  • @user1017524 to answer that you need to show your source code... otherwise I would just speculate... – Yahia Feb 05 '12 at 16:22
  • How would I set up an if statement inside my sub to check if the button was clicked? – user1017524 Feb 05 '12 at 16:29
  • @user1017524 I don't understand... you say you are using C# - in C# there is nothing called sub. you are talking about some `if`statement - why don't you use an event handler ? – Yahia Feb 05 '12 at 16:34
  • Nevermind I figured it out. I created an int, made it equal to 0. Then when the stop button is clicked the int = 1. So in my request I set up an if statement saying if the value of the int is greater than 0 to abort. Then I make the value of the int = to 0 again :) – user1017524 Feb 05 '12 at 16:37
2

You could use the HttpWebRequest.Abort() method to stop your HTTP request.

Please see the following MSDN link for an example.

Hans
  • 12,902
  • 2
  • 57
  • 60