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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:30:32+00:00 2026-06-07T03:30:32+00:00

As you might know Thread class has IsAlive property. It is false if the

  • 0

As you might know Thread class has IsAlive property. It is false if the thread method returned or the thread was aborted. So I have an issue with this:

I have a windows service that starts several tasks in different threads. It permanently checks IsAlive property of the threads and if it is false – recreates a task:

    foreach (var worker in _workers)
        _threads.Add(new Thread(worker.ProtectedRun));

    foreach (var thread in _threads)
        thread.Start();

    while (!EventWaitHandle.WaitOne(0))
    {
        for (var i = 0; i < _threads.Count; i++)
        {
            if (!_threads[i].IsAlive)
            {
                _threads[i] = new Thread(_workers[i].ProtectedRun);
                _threads[i].Start();
            }
        }

        EventWaitHandle.WaitOne(1000);
    }

But one of the tasks has Timer inside. In ProtectedRun method it starts timer and returns. After method is returned -> Thread’s IsAlive property becomes false -> windows service starts thread again -> infinite loop 🙂

    public override void ProtectedRun()
    {
        _timer = new System.Timers.Timer(24 * 60 * 1000);
        _timer.Elapsed += OnTimedEvent;
        _timer.Enabled = true;
        _timer.Start();
    }

Do someone has an idea how to handle this situation? Maybe check thread status instead of IsAlive property?

  • 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-07T03:30:34+00:00Added an answer on June 7, 2026 at 3:30 am

    Well, you ahve a broken design, simply like that.

    1. You run tasks in threads and restart them when they are not alive anymore. GOod.

    2. You have a specific tasks ending immediately but queueing more work on a timer. Good.

    Bad news is, though, that the one task and the testing under the first premise are nsimply not compatible.

    Choices:

    • Rework the restart logic or

    • Keep the thread alive after starting the timer.

    • Rework it so that there is a specific TASK LEVEL (not thread level) status field to follow.

    At the end, your one ProtectedRun method is not following the specifications it should by returning while having active work queued (via the timer).

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

Sidebar

Related Questions

I know this might be a long shot, but here it goes. I have
I know this might be very easy to some,, I have a simple string
I know it lot of people have problems with this topic and you might
I have the start of an android game. It has a GameLoop (thread) class
I know this might sound easy. I thought about using the first dot(.) which
I know this might seem like a duplicate, but I'm not getting anywhere with
I know this might be a stupid question, but here it goes. I always
I know this might be a bit awkward but I am trying to modify
I know this might seem a controversial question but it really is not meant
I know this might sounds not reasonable, and might be a bad design. But

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.