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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:52:20+00:00 2026-05-11T10:52:20+00:00

I was trying to stop some threads, read some things about the proper way

  • 0

I was trying to stop some threads, read some things about the proper way to do it gracefully, but I must be doind something wrong because it simply doesn’t work. At first I tried without the lock() with _IsRunning being volatile, then tried with the locks. Here is what I’ve got.

private volatile bool _IsRunning; private static readonly object runLock = new object();  public void Start() {     if (_IsRunning == true)         return;     _IsRunning = true;     (new System.Threading.Thread(new System.Threading.ThreadStart(SendLoop))).Start(); }  public void Stop() {     lock (runLock)     {         _IsRunning = false;     } }  private void SendLoop() {     while (_IsRunning)     {         lock (runLock)         {             if (_sockets.Count > 0)             {                 //some stuff             }             else             {                 System.Threading.Thread.Sleep(10);             }         }     } } 

I set a breakpoint at my while(), and _IsRunnig is still true even though I passed in Stop().

  • 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-11T10:52:21+00:00Added an answer on May 11, 2026 at 10:52 am

    The lock is required here because of the way your start method is written, however, you only need the lock in Start() (where it isn’t now) and Stop(), since they’re the only ones that could potentially cause a race condition in your case.

    I would remove the lock from your SendLoop() method entirely (it’s causing a DeadLock since Stop is waiting on the lock to set _isRunning, and your SendLoop is holding the lock until _isRunning is set to false). Right now, when you call Stop(), the lock is preventing it from ever setting _isRunning = false;

    However, you will need locks in your Start() and Stop() methods (unless you rework the way they are structured entirely). Something like:

    public void Start() {     lock (runLock)     {         if (_IsRunning == true)             return;         _IsRunning = true;         (new System.Threading.Thread(new System.Threading.ThreadStart(SendLoop))).Start();     } }  public void Stop() {     lock (runLock)     {         _IsRunning = false;     } } 

    This will protect you from starting 2 threads, and will also keep Stop from stopping before the thread has started.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The MS Office apps do a lot of calculation in… May 12, 2026 at 2:27 pm
  • Editorial Team
    Editorial Team added an answer If the code does what it's supposed to, I think… May 12, 2026 at 2:27 pm
  • Editorial Team
    Editorial Team added an answer On Linux, malloc() will never fail -- instead, the OOM… May 12, 2026 at 2:27 pm

Related Questions

I have encountered a strange Invalid Packet Lenght (that is how the error is
I want the user to simply be able to exit my application. However, for
I have a GUI C# application that has a single button Start/Stop. Originally this
I have a list, each item of which has several things in it, including

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.