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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:20:39+00:00 2026-05-12T22:20:39+00:00

Here is the scenario, i opened my application then the tray icon shows, if

  • 0

Here is the scenario, i opened my application then the tray icon shows, if i double-clicked the tray icon, the main interface will be shown. if i open again my application, the main interface should be given focus or if it is not yet shown then it should be shown instead of opening another instance of my app.

here is how my code looks like:

//Program.cs
.....

if(myAppIsNotRunningYet) //the program has not been open yet
{
MyTray = new MyTray();
Application.Run();
}

else //the program is already on the tray
{
//the code to give focus to the mainForm or open it up if not yet open
}

//MyTray.cs
.....
public MyTray()
{
notifyIcon = new NotifyIcon();
....
notifyIcon.Visible = true;
}

private void notifyIcon_DoubleClick(object sender, EventArgs e)
{
MainForm mainForm = new MainForm();
mainForm.ShowDialog();
}
  • 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-12T22:20:39+00:00Added an answer on May 12, 2026 at 10:20 pm

    EDIT: OK, It seems that to override WndProc correctly, you have to use a form which is/has been visible. So below is a different solution using a MessageFilter. This does work, so hopefully you are good to go from here!

     internal sealed class Program
     { 
      /// <summary>
      /// Program entry point.
      /// </summary>
      [STAThread]
      public static void Main(string[] args)
      {
        bool newMutex;
        System.Threading.Mutex mutex = new System.Threading.Mutex(true, "{9F6F0AC4-B9A1-45fd-A8CF-72F04E6BDE8F}", out newMutex);
    
         // Attempt aquire the mutex
         if(newMutex)
         {
          // If we are able to aquire the mutex, it means the application is not running.
             Application.EnableVisualStyles();
             Application.SetCompatibleTextRenderingDefault(false);
    
             // Create the new tray icon
             MyTray myTray = new MyTray();
    
             Application.AddMessageFilter(myTray);
             Application.Run();
    
             // Release the mutex on exit
             mutex.ReleaseMutex();
         }
         else
         {
            // If the aquire attempt fails, the application is already running
            // so we broadcast a windows message to tell it to wake up.
             NativeMethods.PostMessage((IntPtr)NativeMethods.HWND_BROADCAST, NativeMethods.WM_SHOWME, IntPtr.Zero, IntPtr.Zero);
         }
         }
     }
    }
    
    internal class NativeMethods
     {
         public const int HWND_BROADCAST = 0xffff;
         public static readonly int WM_SHOWME = RegisterWindowMessage("WM_SHOWME");
    
         [DllImport("user32")]
         public static extern bool PostMessage(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam);
    
         [DllImport("user32")]     
         public static extern int RegisterWindowMessage(string message);
     }
    
     public class MyTray : IMessageFilter
     {
      private NotifyIcon notifyIcon = new NotifyIcon();
      private Form myForm = new Form();
    
      public MyTray()
      {
         this.notifyIcon.Icon = System.Drawing.Icon.FromHandle(new System.Drawing.Bitmap(16,16).GetHicon());
         this.notifyIcon.Visible = true;
         this.notifyIcon.DoubleClick += delegate(object sender, EventArgs e) { ShowForm(); };
      }
    
         void ShowForm()
         {
            this.notifyIcon.Visible = false;            
            this.myForm.ShowDialog();   
            this.notifyIcon.Visible = true;
          }
    
        public bool PreFilterMessage(ref Message m)
        {
           // If the message is the 'show me' message, then hide the icon and show the form.
           if(m.Msg == NativeMethods.WM_SHOWME)
           {
                if (!this.myForm.Visible)
                {
                    ShowForm();
                    return true; // Filter the message
                }
           }
    
           return false; // Forward the message
        }
     }
    

    EDIT: I put together a sample which is closer to your scenario:

     internal sealed class Program
     {
      static System.Threading.Mutex mutex = new System.Threading.Mutex(true, "{8F6F0AC4-B9A1-45fd-A8CF-72F04E6BDE8F}");
    
      /// <summary>
      /// Program entry point.
      /// </summary>
      [STAThread]
      private static void Main(string[] args)
      {
       // Attempt aquire the mutex
             if(mutex.WaitOne(TimeSpan.Zero, true))
             {
              // If we are able to aquire the mutex, it means the application is not running.
                 Application.EnableVisualStyles();
                 Application.SetCompatibleTextRenderingDefault(false);
    
                 // Create the new tray icon
                 MyTray myTray = new MyTray();
    
                 Application.Run();
    
                 // Release the mutex on exit
                 mutex.ReleaseMutex();
             }
             else
             {
                // If the aquire attempt fails, the application is already running
                // so we broadcast a windows message to tell it to wake up.
                 NativeMethods.PostMessage((IntPtr)NativeMethods.HWND_BROADCAST, NativeMethods.WM_SHOWME, IntPtr.Zero, IntPtr.Zero);
             }
         }
     }
    
     internal class NativeMethods
     {
         public const int HWND_BROADCAST = 0xffff;
         public static readonly int WM_SHOWME = RegisterWindowMessage("WM_SHOWME");
    
         [DllImport("user32")]
         public static extern bool PostMessage(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam);
    
         [DllImport("user32")]     
         public static extern int RegisterWindowMessage(string message);
     }
    
     public class MyTray : Control
     {
      private NotifyIcon notifyIcon = new NotifyIcon();
    
      public MyTray()
      {
       this.notifyIcon.Visible = true;
      }
    
      /// <summary>
      /// This method listens to all windows messages either broadcast or sent to this control
      /// </summary>
      protected override void WndProc(ref Message m)
      {
       // If the message is the 'show me' message, then hide the icon and show the form.
       if(m.Msg == NativeMethods.WM_SHOWME)
       {
        this.notifyIcon.Visible = false;
        using (Form mainForm = new Form())
        {
         mainForm.ShowDialog();
         this.notifyIcon.Visible = true;
        }  
       }
       else
       {
        base.WndProc(ref m);    
       }   
      }
     }
    

    EDIT: I found an example in C# that combines the mutex and windows message:

    C# .NET Single Instance Application


    A mutex is probably the best way to go. Combine this with a custom windows message which your application listens for to bring itself into focus (see VB.NET, VB6 and C# Interprocess communication via Window Messaging).

    Check out this sample:
    C# – Single Application Instance

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

Sidebar

Related Questions

I am facing a scenario here. I have a main class which initializes a
I will have couples scenario here. Please help me Here is the base case:
Here is the scenario: ThickBox is opened from parent window (when button is pressed)
Tackling a strange scenario here. We use a proprietary workstation management application which uses
This seems impossible but I will ask anyway. Here is the scenario I have
I'm registering my iPhone application for Remote Notification. Here my scenario: User opens my
I know how to write tab host,but here the scenario is little bit different
Here's my scenario: <!-- Normal Control --> <div class=required> <label for=address1>Address line 1</label> <input
here is my scenario our team develops on AIX dozens of applications, mostly Perl,
Here's the scenario. I'm using a url rewriter that allows me to specify patterns

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.