1

The app I am creating includes a notify icon and it is in a way that stays active in the background (well, sort of). The problem is that when the user clicks on the icon of my app, the app will launch again, and so I have two notification icons and two activities in the background and two of everything. And if the user clicks the icon again, there will be three of all those things and...... so somehow I should stop this. I have kind of an idea of setting a flag and setting its value to registry, and when my app wants to start, in the form load event, by reading the value, terminates the app. But I want a more... professional way to deal with it. For example not being started at all.

Gabe
  • 84,912
  • 12
  • 139
  • 238
roostaamir
  • 1,928
  • 5
  • 24
  • 51

2 Answers2

1

create some form of Global Mutex/Event handle that your app creates and then in your open routine check for it being present, if it is, exit the startup routing.

I have some basic code here in this answer I gave (for a different issue), but it demonstrates what you are trying to do.

UnauthorizedAccessException on Openexisting global mutex

Community
  • 1
  • 1
Paul Farry
  • 4,730
  • 2
  • 35
  • 61
  • could you please talk in a way that you're talking to a somewhat amateur?!perhaps with a code sample or something?cause this is somewhat my first real app!thanks – roostaamir Mar 03 '13 at 14:13
  • Just to be clear, when you fire up a new instance of an exe, it opens in a different STA. They don't share variables, so you cannot use mutexes or events, as they're not shared. Am I mistaken? – Mike C. Mar 03 '13 at 14:29
  • 1
    GLOBAL\Mutex are visible across the entire Windows machine. You will need to grant permissions if you want to be able to attach to them but they give Exceptions if they exist and you don't have permission. – Paul Farry Mar 03 '13 at 14:37
  • Thanks for the extra info. http://stackoverflow.com/questions/229565/what-is-a-good-pattern-for-using-a-global-mutex-in-c/229567 looks like a good SO post with an implementation. – Mike C. Mar 03 '13 at 14:45
1

this is from this answer which is a duplicate of this one. You check in your app's entry constructor for whether or not the app is already running.

static void Main() 
{
    Process currentProcess = Process.GetCurrentProcess();
    var runningProcess = (from process in Process.GetProcesses()
                          where
                            process.Id != currentProcess.Id &&
                            process.ProcessName.Equals(
                              currentProcess.ProcessName,
                              StringComparison.Ordinal)
                          select process).FirstOrDefault();
    if (runningProcess != null)
    {
        ShowWindow(runningProcess.MainWindowHandle, SW_SHOWMAXIMIZED);
       return; 
    }
}
Community
  • 1
  • 1
Mike C.
  • 3,024
  • 2
  • 21
  • 18
  • you see,What I am saying is clicking the app itself(not its notify icon) brings this case.I mean when click on the app.exe file of mine,this problem occurs . – roostaamir Mar 03 '13 at 14:17
  • I see when he says clicks the icon, he's talking about the actual desktop icon. Not sure if I have that code hanging around anywhere. I'll delete this if I can't find it. – Mike C. Mar 03 '13 at 14:18