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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:52:59+00:00 2026-05-16T04:52:59+00:00

I have a custom ApplicationContext and I’m trying to terminate it if specific conditions

  • 0

I have a custom ApplicationContext and I’m trying to terminate it if specific conditions are met. I am using a Mutex to ensure a single instance.

I’ve tried base.OnMainFormClosed(null, null);. Application.Exit() and ExitThread. Everything stops processing, but the process itself is still running.

Complete Main() method:

static void Main()
    {
        bool firstInstance;
        using (Mutex mutex = new Mutex(true,
                                   @"Global\MyApplication",
                                   out firstInstance))
        {
            if (!firstInstance)
            {
                MessageBox.Show("Another instance is already running.");
                return;
            }

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            Application.Run(new CustomContext());
        }
    }

What’s the correct technique?

  • 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-16T04:53:00+00:00Added an answer on May 16, 2026 at 4:53 am
       Application.Run(new CustomContext());
    

    That’s okay, but you don’t store a reference to the CustomContext object you created. There’s thus no way to call its ExitThread method. Tweak it like this:

    class Program {
        private static CustomContext appContext;
    
        [STAThread]
        public static void Main() {
           // Init code
           //...
           appContext = new CustomContext();
           Application.Run(appContext);
        }
        public static void Quit() {
            appContext.ExitThread();
        }
    }
    

    Now you can simply call Program.Quit() to stop the message loop.

    Check my answer in this thread for a better way to implement a single-instance app. The WindowsFormsApplicationBase class also offers the ShutdownStyle property, probably useful to you instead of ApplicationContext.

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

Sidebar

Related Questions

my application is using spring applicationContext.xml. i have a custom filter class ( CustomFilter
We have custom headers in the Silverlight DataGrid using the ContentTemplate. We've got a
I have custom map image with specific height and width. i need to map
I have a working 3.0.2 applicationContext-security.xml that uses a custom authenticator <global-method-security pre-post-annotations=disabled> </global-method-security>
I have custom checkstyle checks file (called checks.xml), and I'm trying to use that
I have custom objects I am binding to using ObjectDataSource. I have three-level binding:
I have a custom ServletContextListener that I am using to initialize and start up
I have custom UITableViewCell . It contains UITextLabel . When I press this cell
I have custom validation rule: public function customRule($check) { } Inside this rule I
I have custom classes that I currently instantiate within App.xaml as resources. I want

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.