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 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

Ask A Question

Stats

  • Questions 89k
  • Answers 89k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I'm not exactly sure where to look in the source… May 11, 2026 at 5:52 pm
  • Editorial Team
    Editorial Team added an answer Caching is definitely the way to go, use a Hashtable… May 11, 2026 at 5:52 pm
  • Editorial Team
    Editorial Team added an answer If you really want to do this, you can use… May 11, 2026 at 5:52 pm

Related Questions

I'm creating a windows service and after installing the service, it stops and starts
I am making an app that scrolls information on the desktop. The window is
I as of yet, have not had a need to create a dll in
I am supposed to provide my users a really simple way of capturing video

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.