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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:56:02+00:00 2026-05-13T13:56:02+00:00

I am currently working on an application in C# that runs on an infinite

  • 0

I am currently working on an application in C# that runs on an infinite loop with a Thread.Sleep call after each iteration of the other method calls. My main is –

static void Main(string[] args)
    {
        bool isOnlyInstance = false;
        Mutex mutex = new Mutex(true, "RiskMetricsSensitivitiesDatabaseLoader", out isOnlyInstance);

        if (!isOnlyInstance)
        {
            return;
        }

        while (true)
        {
            ProcessData();
            Thread.Sleep(MainLoopSleep);
        }

        GC.KeepAlive(mutex);
    }

I have inserted the KeepAlive call at the end of the method to ensure the singleton mutex works as expected, as outlined by various websites. The call to KeepAlive is supposed to keep garbage collection from throwing away the mutex, since .NET looks forward to anticipate/optimize garbage collection.

My question is, since the actual call to KeepAlive never gets reached, should I put it in the loop after Thread.Sleep? The compiler warns that KeepAlive never gets called, and I’m concerned that it will therefore ignore this line in my garbage collection prevention algorithm.

  • 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-13T13:56:03+00:00Added an answer on May 13, 2026 at 1:56 pm

    Mutex is disposable. Why not wrap it in a using?

    using(new Mutex(blah, blah, blah)){
        while(true){
            Blah(blah);
        }
    }
    

    Note that this works without even assigning the new Mutex to a named variable.

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

Sidebar

Related Questions

I am currently working on an application that will retrieve other users' locations based
Am currently working on an application that requires users to submit posts and comments
I'm currently working in an application that has to navigate a webpage and recollect
I am currently working on an application that has different permissions/users for the local
I'm currently working on an application that allows people to schedule Shows for an
I am currently working on an application that allows reverse geocoding using silverlight +
I am currently working on an application that uses a TableViewer in several places
I am currently working on a GWT application that requires report printing. The use
I'm currently working on an ASP.Net WebForms application that allows a user to select
I'm currently working on a PHP application that uses a MySQL database for its

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.