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 6604825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:12:31+00:00 2026-05-25T19:12:31+00:00

I have this tricky task I’ve been trying to achieve for quiet sometime but

  • 0

I have this tricky task I’ve been trying to achieve for quiet sometime but till now I couldn’t think of anything to make it work. anyway here is the scenario…

I have a winform application contains a listview and a button.
the listview contains 1 column which holds the data I need to pass to my functions later on. the column contains lets say 50 rows containing a list of links.

Now I have this function which I’m using to fetch and grab the contents of these links (5 links at a time) with parallel multithreaded mode using (Task Parallel Library):

//List<int> currentWorkingItem //contains the indices of the items in listview
//List<string> URLsList //contains the URLs of the items in listview

Parallel.ForEach(URLsList, new ParallelOptions() { MaxDegreeOfParallelism = 5 }, (url, i, j) =>
{
    //show to user this link is currently being downloaded by highlighting the item to green...
    this.BeginInvoke((Action)(delegate()
    {
        //current working item 
        mylistview.Items[currentWorkingItem[(int)j]].BackColor = green;

    }));

    //here I download the contents of every link in the list...
    string HtmlResponse = GetPageResponse(url);

    //do further processing....
});

Now the above code works perfectly… but sometimes I want the user to abort certain thread which is currently running and continue with the rest of the threads in the list… is that achievable in this? if so please help me out.. I’d really appreciate any solution or suggestions..

  • 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-05-25T19:12:31+00:00Added an answer on May 25, 2026 at 7:12 pm

    ok after struggling with this I finally found an efficient and an easy solution for this..

    it required me only a hashtable which contains the indicies of the selected items in the listview and a simple bool value. the index is the key and the bool (true, false) is the value. the bool value is like an (on/off) switch indicates that the current loop is aborted or not.. so in order to abort specific thread simple I need to pass the key(the index) of the selected item on my listview to the foreach loop and check if the bool switch is on or off and that’s basically it…

    so my final code will be like this:

    //I declared the hashtable outside the function so I can manage it from different source.
    
        private Hashtable abortingItem;
    

    Now when I click grab button it should fill the hashtable with the selected indicies…

    abortingItem = new Hashtable();
    
    for (int i = 0; i < myURLslist.SelectedItems.Count(); i++)
    {
        //false means don't abort this.. let it run
        abortingItem.Add(myURLslist.SelectedItems[i].index, false);
    }
    
    //here should be the code of my thread to run the process of grabbing the URLs (the foreach loop)
    //..........................
    

    now if I need to abort specific item all I need is to select the item in the listview and click abort button

    private void abort_Click(object sender, EventArgs e)
    {
        if (abortingItem != null)
        {
            for (int u = 0; u < myURLslist.SelectedIndices.Count; u++)
            {
                //true means abort this item
                abortingItem[myURLslist.SelectedIndices[u]] = true;
            }
        }
    }
    

    In my foreach loop all I need is a simple if else statement to check if the bool is on or off:

    //List<int> currentWorkingItem //contains the indices of the items in listview
    //List<string> URLsList //contains the URLs of the items in listview
    
    Parallel.ForEach(URLsList, new ParallelOptions() { MaxDegreeOfParallelism = 5 }, (url, i, j) =>
    {
    
    //aborting
    if (!(bool)abortingItem[currentWorkingItem[(int)j]])
    {
        //show to user this link is currently being downloaded by highlighting the item to green...
        this.BeginInvoke((Action)(delegate()
        {
            //current working item 
            mylistview.Items[currentWorkingItem[(int)j]].BackColor = green;
    
        }));
    
        //here I download the contents of every link in the list...
        string HtmlResponse = GetPageResponse(url);
    
        //do further processing....
    }
    else
    {
      //aborted
    }
    });
    

    that’s simply it..

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

Sidebar

Related Questions

I have this very tricky problem and Im trying to figure it out for
This is a little tricky so bear with me: I have a PHP script
We have this software that has a webservices component. Now, the administrator of this
I have a really tricky task to do: there is an existing web project
I have a task, which I know how to code (in C#), but I
I've been looking through the R Task Views a lot lately and have found
Have this query: SELECT HOUR( DATE ) AS hr, COUNT( * ) AS cnt
Have this self-made slider: http://jsfiddle.net/wyc3P/4/ What it does: takes min and max values in
I have this array in PHP: array( [0] => array( 'username' => 'user1' )
I have this error message: Msg 8134, Level 16, State 1, Line 1 Divide

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.