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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:38:02+00:00 2026-05-25T19:38:02+00:00

I wish to stop a thread from entering code while another thread is still

  • 0

I wish to stop a thread from entering code while another thread is still executing that bit of code:

I’m currently going the following:
global.asax.cs

    private static bool _isProcessingNewIllustrationRequest;

    public static bool IsProcessingNewIllustrationRequest
    {
        get { return _isProcessingNewIllustrationRequest; }
        set { _isProcessingNewIllustrationRequest = value; }
    }

Then in MVC:

    public ActionResult CreateNewApplication()
    {
        if (!Global.IsProcessingNewIllustrationRequest)
        {
            Global.IsProcessingNewIllustrationRequest = true;

            // DO WORK... RUN CODE
            Global.IsProcessingNewIllustrationRequest = false;
            return View("Index", model);
        }
        else
        {
            // DISPLAY A MESSAGE THAT ANOTHER REQUEST IS IN PROCESS
        }
    }

But if seems as if the code isn’t working, because both threads(Chrome & Firefox) still executes the code at the same time.

UPDATED

private Object thisLock = new Object();     
public ActionResult CreateApplication()
        {
            ILog log = LogManager.GetLogger(typeof(Global));
            string aa = this.ControllerContext.HttpContext.Session.SessionID;
            log.Info("MY THREAD: " + aa);

            lock (thisLock)
            {
                Thread.Sleep(8000);

                DO SOME STUFF

            }
        }

Even with the log, thread 2 (Firefox session) still goes into the code while session1 (Chrome) is executing it with the lock. What am I missing?

  • 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-25T19:38:02+00:00Added an answer on May 25, 2026 at 7:38 pm

    I can see two obvious reasons why this code won’t do what you want.

    Problem #1: It’s not threadsafe. Every time someone connects to your server, that request is going to run in a thread, and if multiple requests come in, then multiple threads are all going to be running at the same time. Several threads could very well read the flag at the same time, all see that it’s false, and so all enter their if block and all do their work, which is exactly what you’re trying to prevent.

    This is a “race condition” (the results depend on who wins the race). Boolean variables aren’t enough to solve a race condition.

    There are a bunch of ways to actually fix this problem. The best would be to remove the shared state, so each thread can run completely independently of the others and there’s no need to lock the others out; but I’ll assume you’ve already considered that and it’s not practical. The next things to look at are mutexes and monitors. Monitors are a little simpler to use, but they only work if both threads are actually in the same appdomain and the same process. That brings me to…

    Problem #2: The two threads might not be in the same process. Recent versions of IIS will launch multiple separate worker processes to handle Web requests. Each process has its own address space, meaning each process (technically each appdomain within each process) has its own copy of your “global” variable!

    In fact, if you’re building a really high-traffic Web site, the different worker processes may not even run on the same computer.

    Worker processes are your most likely culprit. If you were facing a race condition, the problem would be incredibly rare (and even more incredibly hard to debug when it did come up). If you’re seeing it every time, my money is on multiple worker processes.

    Let’s assume that all the worker processes will be on the same server (after all, if you had the kind of budget that required apps that can scale out to multiple Web servers, you would already know way more about multithreaded programming than I do). If everything’s on the same server, you can use a named Mutex to coordinate across different appdomains and processes. Something like this:

    public static Mutex _myMutex = new Mutex(false, @"Global\SomeUniqueName");
    
    public ActionResult CreateNewApplication()
    {
        if (_myMutex.WaitOne(TimeSpan.Zero))
        {
            // DO WORK... RUN CODE
            _myMutex.ReleaseMutex();
            return View("Index", model);
        }
        else
        {
            // DISPLAY A MESSAGE THAT ANOTHER REQUEST IS IN PROCESS
        }
    }
    

    The WaitOne(TimeSpan.Zero) will return immediately if another thread owns the mutex. You could choose to pass a nonzero timespan if you expect that the other thread will usually finish quickly; then it will wait that long before giving up, which would give the user a better experience than failing instantly into a “please try again later” error message.

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

Sidebar

Related Questions

I've a thread that runs following tcpConnect method. I wish to stop it at
I wish to stop tracking files but still keep them in my working tree.
I like that it installs into ~/.gem , I just wish it would stop
I wish to stop a token parser when the semantic action code finds a
I've an app that has multiple accounts and SyncAdapter. I wish to stop Syncing
I wish Subversion had a better way of moving tags. The only way that
I wish to implement a 2d bit map class in Python. The class would
I wish to implement my software on a shareware basis, so that the user
I wish to test a function that will generate lorem ipsum text, but it
I wish to migrate the database of a legacy web app from SQL Server

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.