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

  • Home
  • SEARCH
  • 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 7652725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:50:42+00:00 2026-05-31T11:50:42+00:00

well, i make a loop that makes a lot of threads, see: foreach (DataGridViewRow

  • 0

well, i make a loop that makes a lot of threads, see:

foreach (DataGridViewRow dgvRow in dataGridView1.Rows)
{

    Class h = new Class(dgvRow.Cells["name"].Value.ToString());

    Thread trdmyClass = new Thread(h.SeeInfoAboutName);
    trdmyClass.IsBackground = true;
    trdmyClass.Start();
}

This is working fine, creating the threads that i need, but i want to stop all this threads (using Thread.Abort()), in one time when i click on a button for e.g.
How can i do this?

  • 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-31T11:50:43+00:00Added an answer on May 31, 2026 at 11:50 am

    You really shouldn’t use Thread.Abort(), it can be very dangerous. Instead, you should provide some way to signal to the threads that they are canceled. Each thread would then periodically check whether it’s canceled and end if it was.

    One way to do this would be to use CancellationToken, which does exactly that. The framework methods that support cancellation work with this type too.

    Your code could then look something like this:

    // field to keep CancellationTokenSource:
    CancellationTokenSource m_cts;
    
    // in your method:
    
    m_cts = new CancellationTokenSource();
    
    foreach (DataGridViewRow dgvRow in dataGridView1.Rows)
    {
        Class h = new Class(dgvRow.Cells["name"].Value.ToString());
    
        Thread trdmyClass = new Thread(() => h.SeeInfoAboutName(m_cts.Token));
        trdmyClass.IsBackground = true;
        trdmyClass.Start();
    }
    
    //somewhere else, where you want to cancel the threads:
    m_cts.Cancel();
    
    // the SeeInfoAboutName() method
    public void SeeInfoAboutName(CancellationToken cancellationToken)
    {
        while (!cancellationToken.IsCancellationRequested)
        {
            // do some work
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It's a well known problem that executing make test doesn't build the tests as
I'm trying to make a loop that will recurse through an array of bytes
Well my project requires me for make a link list within a link list.
I have a menu working fairly well but can't figure out how to make
The question and description has been edited to make the user understand well what
I'm trying to make a design decision. I'm not well acquainted with either MongoDB
I am reading a C book that deals a lot with asm and looking
So i'm trying to make a program that can find how many pixels of
I'm writing a stored procedure that needs to have a lot of conditioning in
I have an app that runs 2 threads in loops. 1st one is updating

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.