Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8400639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:36:30+00:00 2026-06-09T21:36:30+00:00

I have some a bit of code following : On Form : List<WebRequestUri> lwebrequest

  • 0

I have some a bit of code following :

On Form:

    List<WebRequestUri> lwebrequest = new List<WebRequestUri>();

    public Form1()
    {
        InitializeComponent();
    }

    private void Start_Click(object sender, EventArgs e)
    {
        Thread thread = new Thread(new ParameterizedThreadStart((object context) =>
            {
                DoWork();
            }));
        thread.IsBackground = true;
        thread.Start();            
    }

    void DoWork()
    {
        lwebrequest = new List<WebRequestUri>();
        for (int i = 0; i < 100; i++)
        {
            WebRequestUri wp = new WebRequestUri();
            wp.Start();
            lwebrequest.Add(wp);
        }
    }

    private void Stop_Click(object sender, EventArgs e)
    {                  
        for (int i = 0; i < lwebrequest.Count; i++)
        {               
            lwebrequest[i].Abort();
        }
    }

Worker Class:

class WebRequestUri
{
    Thread thread = null;
    WebRequest webRequest = null;
    WebResponse webResponse = null;
    StreamReader sr = null;

    public void Start()
    {
        thread = new Thread(new ParameterizedThreadStart((object context) =>
            {
                SendRequest();
            }));
        thread.IsBackground = true;
        thread.Start();
    }

    public void Abort()
    {            
        if (webResponse != null) webResponse.Close();
        if (webRequest != null) webRequest.Abort();
        if (thread != null) thread.Abort();            
    }

    public void SendRequest()
    {
        webRequest = WebRequest.Create("http://google.com");

        webRequest.ContentType = "application/x-www-form-urlencoded";
        webRequest.Method = "GET";         
       try
        {
            webRequest.BeginGetResponse(new AsyncCallback(GetResponseCallback), webRequest);
        }
        catch (WebException)
        {
        }
    }

    private void GetResponseCallback(IAsyncResult asynchronousResult)
    {
        try
        {
            webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
            webResponse = (HttpWebResponse)webRequest.EndGetResponse(asynchronousResult);
            sr = new StreamReader(webResponse.GetResponseStream());
            string response = sr.ReadToEnd();
            Console.Write(response);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }
}

I’m getting a problem that when i try to click stop button. I see my form is blocked. I’m not sure that my stop function right or wrong. Can you give me some advice? Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T21:36:32+00:00Added an answer on June 9, 2026 at 9:36 pm

    Possible causes for the hang have been suggested by others.
    The more important thing however is the actual usage of Thread.Abort.

    It is considered a very bad thing to do and you won’t believe the amount of issues and subtle bugs it can cause.

    The good news is that you usually don’t really need it, untimely completion of asynchronous tasks can and should be handled by the application itself. Consider your use case for example:

    What are you trying to accomplish by calling abort? Are you trying to abort the request? you can’t because it is already under way. it will reach its destination and be processed regardless of whether there is somebody on the other side waiting for it.
    If however, you are trying to avoid unnecessary calculations on your side- you have full control. You can keep a Boolean member in the class named “abortRequested” and the Abort method would just set it to “true”. The method that you want to abort needs to check this member from time to time in order to decide whether it should proceed execution.

    We can analyze your use case further if you want.

    EDIT:
    It would look something like this:

    bool stopped = false; // can make this thread safe if you want.
    
    // Assuming you have a computation in a loop.
    compouteAsynch(){
    
       for(var workItme in workItems){
         if(!stopped){
          dostuff(workItem)
         }
       }
    }
    
    void stop(){
       stopped = true; // work will not stop immediately. Only in the next iteration.
    }
    

    You can control the granularity of the ‘stopped’ sampling as you like. However you must be aware that it is not possible to sample it everywhere. For example- you cant check it while the thread is executing a query against a DB. This should be good enough though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following bit of code that triggers some ajax events on a
I need to integrate some legacy 32-bit code - for which I don't have
I have some code here that uses bitsets to store many 1 bit values
I have some legacy C++ code that I am trying to understand a bit
I use this bit of code to feed some data i have parsed from
I am a bit new with android but I have some java experience. So
I have the following code: (some.aspx.cs) if(Page.IsPostBack) { bool apple2 = false; bool pizza2
I have some IronPython code that is creating a Mutex using the following constructor:
I have the following code: [HttpGet] public ActionResult Edit(int req) { var viewModel =
I have a class and I want to have some bit masks with values

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.