[.net] C# Tray Icons, and only allowing one instance of an application.

Started by
4 comments, last by Koori 15 years, 4 months ago
Hello, What i'm trying to do, is have an application, that when you close it, it minimizes to the tray icon (similar to Google Talk). And then if someone is to open up the application again, if it's in the tray icon, it would just restore it, rather than open up a new instance. I got the easy part down (closing to the windows tray), but i'm not sure where to start with the part of opening a new instance. Here is what i'm doing currently Psudeo-Code

//basic application constructor
Application()
{

bool diffAppRunning = CheckForADifferentInstanceOfApplication();

if (diffAppRunning)
{
    RestoreDifferentApplication();
    CloseThisApplication();
}
}

I think I can find out if the application is already running by checking the process' or what ever, but as for telling the other application to restore itself again, I'm lost. Can anyone help me out, or possiblibly just give me some keywords to google search for or something? If what I'm doing doesn't make sense I'll try to post back and explain it a bit better. Thanks, ArchG
Advertisement
Here's a snippet from an old program of mine that allows you to detect whether an instance of your app is already running or not.

Basically, it creates a named mutex, which is a system-wide synchronization primitive, and then acquires exclusive ownership of it. When a second instance of your application performs the same steps, its attempt to acquire ownership of the mutex will fail. That allows you to detect that the app is already running.

Instead of "MyFabulousApp" you could, for example, use the full name of the executing assembly.

using System.Threading;public static void Main(string[] args){  Mutex mtx = new Mutex(false, "MyFabulousApp");  try  {    if (!mtx.WaitOne(0, false))    {      MessageBox.Show(null, "An instance of MyFabulousApp is already running ...",        "Aborting", MessageBoxButtons.OK, MessageBoxIcon.Stop,        MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification);      return;    }  }  finally  {    mtx.Close();  }}


As for notifying the other instance to pop up: Maybe you can think of away to make its window handle available. You could then send a window message to restore the window.
Thank you very much! ++rating to you.

The mutex thing worked great.

As far as getting the handle of the window, this is what I used to get the handle.

...if (!mutex.WaitOne(0, false)){         Process[] processes = Process.GetProcessesByName("MyApplication");         foreach (Process p in processes)         {                           IntPtr pFoundWindow = p.MainWindowHandle;              //win32 call              ShowWindow(pFoundWindow, 5);         }                             return;}...


Which works great, except when I hide the window, I can't get the handle of it anymore to restore the window, it always returns 0, making it rather difficult to restore it....still fiddling around with it, i'll post back if I get it.

Thanks Again,
ArchG
You'll need to use some kind of IPC. What version of .NET are you using? If you're using 3.5, there's the System.IO.Pipes namespace which contains a managed interface to Window's named pipes infrastructure. You can use the namedpipes to not only detect the presences of your previous instances, but you can also send a message over the pipe to tell the original instance to restore itself.

If you're not using .NET 3.5 yet, you could do a similar thing by creating a separate mutex that the first application waits on, and then the second application can set it to indicate that the first should restore itself.

Trying to do this by manipulating the Process or windows directly is always going to be error-prone.
There is an easier way to do that kind of thing. Such as this.
Quote:Original post by SiCrane
There is an easier way to do that kind of thing. Such as this.

AFAIK this is the official recommended by Microsoft solution to single-instanced application.

This topic is closed to new replies.

Advertisement