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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:00:20+00:00 2026-06-09T16:00:20+00:00

I have an object in a worker thread, which I can instruct to stop

  • 0

I have an object in a worker thread, which I can instruct to stop running. I can implement this using a bool or an AutoResetEvent:

boolean:

private volatile bool _isRunning;

public void Run() {
    while (_isRunning)
    {
        doWork();
        Thread.Sleep(1000);
    }
}

AutoResetEvent:

private AutoResetEvent _stop;

public void Run() {
    do {
        doWork();
    } while (!_stop.WaitOne(1000));
}

The Stop() method would then set _isRunning to false, or call _stop.Set().

Apart from that the solution with AutoResetEvent may stop a little faster, is there any difference between these method? Is one “better” than the other?

  • 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-09T16:00:22+00:00Added an answer on June 9, 2026 at 4:00 pm

    C# volatile does not provide all the guaranties. It may still read stale data. Better to use underlying OS synchronisation mechanism as it provides much stronger guaranties.

    All this in great depth discussed by Eric Lippert (really worth reading), here is short quote:

    In C#, “volatile” means not only “make sure that the compiler and the
    jitter do not perform any code reordering or register caching
    optimizations on this variable”
    . It also means “tell the processors to
    do whatever it is they need to do to ensure that I am reading the
    latest value, even if that means halting other processors and making
    them synchronize main memory with their caches”
    .

    Actually, that last bit is a lie. The true semantics of volatile reads
    and writes are considerably more complex than I’ve outlined here; in
    fact they do not actually guarantee that every processor stops what it
    is doing
    and updates caches to/from main memory. Rather, they provide
    weaker guarantees about how memory accesses before and after reads and
    writes may be observed to be ordered with respect to each other.

    Certain operations such as creating a new thread, entering a lock, or
    using one of the Interlocked family of methods introduce stronger
    guarantees about observation of ordering. If you want more details,
    read sections 3.10 and 10.5.3 of the C# 4.0 specification.

    Frankly, I discourage you from ever making a volatile field. Volatile
    fields are a sign that you are doing something downright crazy: you’re
    attempting to read and write the same value on two different threads
    without putting a lock in place. Locks guarantee that memory read or
    modified inside the lock is observed to be consistent, locks guarantee
    that only one thread accesses a given hunk of memory at a time, and so
    on.

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

Sidebar

Related Questions

I have method which create background thread to make some action. In this background
I have a main thread which should process data coming from several worker threads.
My co-worker is using a third-party .NET library for which we don't have the
I am using an external component which periodically shoots events from a worker thread.
I have a class that spawns an arbitrary number of worker object that compute
I have object A which contains multiple instances of object B, which in turn
I have shared object A.so which statically links to libssl.a & another shared object
I have this object: stdClass Object ( [daily_inventoryID] => 1 [inventory_timestamp] => 2012-06-08 14:35:42
I have a single thread producer which creates some task objects which are then
I have read that we can not access anything in the main UI thread

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.