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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:06:22+00:00 2026-05-24T02:06:22+00:00

There seems to be a lot to learn about multithreaded programming and it’s all

  • 0

There seems to be a lot to learn about multithreaded programming and it’s all a bit intimidating.

For my current needs, I just want to protect against a method being called again from another thread before it finishes, and my question is:

Is this an adequate (safe) way to make a method thread-safe?

class Foo
{
    bool doingWork;
    void DoWork()
    {
        if (doingWork)  // <- sophistocated thread-safety
            return;     // <-

        doingWork = true;

        try
        {
            [do work here]
        }
        finally
        {
            doingWork = false;
        }
    }
}

If that isn’t sufficient, what is the simplest way to achieve this?


EDIT: More info about the scenario:

  • There is only one instance of Foo

  • Foo.DoWork() will be called from a ThreadPool thread on the Elapsed
    event of a System.Timers.Timer.

  • Normally Foo.DoWork() will finish eons before the next time it’s
    called, but I want to code for the slim chance that it will run long,
    and get called again before finishing.


(I’m also not smart enough to be sure if this question could be tagged language-agnostic, so I haven’t. Enlightened readers, feel free to do so if applicable.)

  • 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-05-24T02:06:23+00:00Added an answer on May 24, 2026 at 2:06 am

    Your code is not thread safe. You should use the lock keyword instead.

    In your current code:

      if (doingWork)
            return;
    
      // A thread having entered the function was suspended here by the scheduler.
    
      doingWork = true;
    

    When the next thread comes through, it will also enter the function.

    This is why the lock construct should be used. It basically does the same as your code, but without the risk for a thread being interrupted in the middle:

    class Foo
    {
        object lockObject = new object;
        void DoWork()
        {
            lock(lockObject)
            {
                [do work here]
            }
        }
    }
    

    Note that this code has somewhat different semantics than your original. This code will cause the second thread entering to wait and then do the work. Your original code made the second thread just abort. To come closer to your original code, the C# lock statement cannot be used. The underlying Monitor construct has to be used directly:

    class Foo
    {
        object lockObject = new object;
        void DoWork()
        {
            if(Monitor.TryEnter(lockObject))
            {
                try
                {
                    [do work here]
                }
                finally
                {
                    Monitor.Exit(lockObject);
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There seems to be a lot of heated discussion on the net about the
Okay, There seems to be a lot of discussion on here about how terribly
How exactly do DLL files work? There seems to be an awful lot of
I've had some experience with Pygame, but there seems to be a lot of
It seems like there is a lot of overhead involved in rapidly opening and
It seems to me like there's a lot of sheeping going on, with everyone
I am working on an interpreter for a toy language, just to learn about
I'm just starting to learn Python and have heard about the GIL and how
I'm looking to learn LINQ, but I'm finding that there is a lot more
There seems to be no good way to localize a WPF application. MSDN seems

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.