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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:30:21+00:00 2026-05-11T11:30:21+00:00

I am starting atmost 5 threads in a program in C# .NET. But sometimes

  • 0

I am starting atmost 5 threads in a program in C# .NET. But sometimes some of those threads just quit or become dead inexplicably even before the completion of execution of the function assigned to it.

It happens randomly . If I try to debug the code by putting breakpoints- It works fine.

And sometimes all the threads execute the assigned functions perfectly.They do not share any resources among them.

            Thread[] td = new Thread[5];             for (count = 4; count >= 0; --count)             {                 ds[count] = dba.getData(ru[count]);                 td[count] = new Thread(delegate() {  runRule[count].performTask(ru[count], ds[count], count); });                 td[count].Name = 'Thread ' + count.ToString();                 td[count].Start();                 Thread.Sleep(50);             } 

If I remove the last line ‘Thread.Sleep(50)’ only the first thread stared runs and rest of them just die.

can someone explain why the threads are becoming dead?

  • 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. 2026-05-11T11:30:21+00:00Added an answer on May 11, 2026 at 11:30 am

    I suspect they’re not becoming dead – I suspect the problem is that you’re not actually running the rules you think you are. When you use a method’s local variables within an anonymous method, the variables themselves are captured. In this case, you’re capturing the count local variable – and then changing it (as the loop counter decreases). By the time a thread created when count=4 starts running, count may be 3 – so it will be calling runRule[3].performTask(ru[3], ds[3], 3). In fact, count could change while the expressions are being evaluated which could cause a lot of fun.

    The way to get round this is to have a different ‘local’ variable for each iteration of the loop. It’s easy to do:

    Thread[] td = new Thread[5]; for (count = 4; count >= 0; --count) {     int copy = count;     ds[count] = dba.getData(ru[count]);     td[count] = new Thread(delegate() {           runRule[copy].performTask(ru[copy], ds[copy], copy);      });     td[count].Name = 'Thread ' + count.ToString();     td[count].Start();     Thread.Sleep(50); } 

    Now the only variables being captured in the delegate are copy and runRule/ru/ds – I’m assuming that the latter three don’t change. A new ‘instance’ of the copy variable is created each time you go round the loop, so changes won’t interfere with each other.

    See whether this helps – it’s at least a potential cause for massive confusion, and may well be the problem.

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

Sidebar

Related Questions

Starting from ASP.NET MVC Preview 3, HTML.Button ( and other related HTML controls) are
Starting recently, some of my new web pages (XHTML 1.1) are setup to do
I'm starting to digg some rails plugins to create modal forms in rails. Almost
Just starting to learn scala.. I can't seem to figure out how to compile
I've written a lot of my code already, but now I'm starting to question
I was trying to do some analysis on calling start and join simultaneously, //Starting
just starting with cakephp and almost with php after a few years without use
I'm starting to learn Ruby on Rails but this question is bugging me. As
I'm starting to design a basic php game, with some properties which can be
-- Question -- I am just starting out with the REST API and am

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.