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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:07:55+00:00 2026-05-28T13:07:55+00:00

I have a Main method that creates a message loop called SysTrayApp: void Main()

  • 0

I have a Main method that creates a message loop called SysTrayApp:

void Main()
{
    Application.Run(new SysTrayApp());
}  

My SysTrayApp class adds a ContextMenu to the System Tray icon, then starts another message loop called MouseHook. MouseHook is a class that pinvokes mouse events. (Modeled after this class.)

class SysTrayApp : Form
{           
    public SysTrayApp()
    {
        // Does some GUI work    
        // Then starts another message loop
        Application.Run(new MouseHook());
    }
}

_

class MouseHook : Form
{           
    public MouseHook()
    {
        // Does some hooking stuff      
    }
}

In SysTrayApp, Application.Exit() is called when the user selects the Exit option from the context menu. The problem is that I have to call Exit twice (or add another Application.Exit) for the application to end. This is expected, but
I feel like I am going about this the wrong way. Calling Application.Exit() twice just seems wrong to me, but I’m new to the C# world. Should SysTrayApp and MouseHook be combined into one big class? MouseHook doesn’t seem like it should be apart of Windows.Form either. Maybe MouseHook needs to be on a new thread? Any suggestions?

EDIT: Oops. MouseHook did not have a third Application.Run()

Solution

I guess I was over thinking this. Still exploring C#, like I said. But here is what works:

Application.Run(new SysTrayApp()); in Main() is all you need. To start MouseHook, just do a MouseHook hook = new MouseHook(); in SysTrayApp.

Thanks @JonSkeet for helping me understand this. You are a machine!

  • 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-28T13:07:55+00:00Added an answer on May 28, 2026 at 1:07 pm

    You’re calling Application.Run in the MouseHook constructor and then when that returns, you’re calling Application.Run again (in the SysTrayApp constructor) – you’re then calling it a third time when the SysTrayApp constructor completes. If you’re going to run three different message loops, you should expect to have to exit three times (I’m amazed you’re only having to call Application.Exit twice).

    You shouldn’t be calling Application.Run in either of your constructors – the idea of a constructor blocking until a message pump has finishes is nasty, IMO. If you really want multiple message loops, they should be on different threads – but I wouldn’t advise you to do that. You should be fine just calling Application.Run in your Main method.

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

Sidebar

Related Questions

I have my main application delegate which contains a method that returns an object.
I have main table called 'Employee' and another slave table called 'EmployeeTypes' that has
There is a thread in my Delphi application that has a message-waiting loop. Every
I have 2 classes within same package. Both classes have main method in them.
For instance in C# or Java, you always have a main() method used to
This is Objective-C, in Xcode for the iPhone. I have a method in main.m:
I have a method. I want to return a value not from the main
i have a table called category in which i have main category ids and
I have a layout that creates a ListActivity. Before displaying the ListActivity, I perform
I have a main activity that takes elements from a database and displays them

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.