0

I would like to set up a persistent state for my application. Let me explain. The startup time is kinda long (mostly due to many database requests to a remote server, which take 5 - 10 seconds, and even more since my users usually have too much applications running...) and I'd like to set up a way to hide & show my application when needed.

What I am doing now is to only reduce app to tray when user clicks on the red cross. (The application really exits only when a user chooses File -> Exit).

All users are launching an installer which is checking the version installed, then the version available online, and update the app if needed before launching it.

Now, I'd like it to first check on the process monitor (the one found in Task Manager, Processes tab), and if a process is already running for the application, it'll just show the window again. Otherwise, if no process is running, we can process the classic-check-for-update-then-launch steps. This would especially remove a lot of stupid customer requests I regularly have ("hey, your application takes too long to load, so I clicked on it again 5 times and it launched 6 instances!!!!" :/ ) and therefore save me a lot of useless time spent asking them to stop launching 50 instances of the same application cause it won't make it any faster...

So my main question is: how to perform such a trick in C#/WPF? For now, my minimization process is kinda simple (even though maybe too simple): I just hide the window & the task bar entry. Now I don't know how to show it back from my installer

Any ideas?

Damascus
  • 6,553
  • 5
  • 39
  • 53
  • What a good idea. I would also say that you could/should have a splash screen appear and give the user feedback that the app is loading, and process those initial queries on a background thread. – Kieren Johnstone Jul 04 '11 at 10:58
  • There is already a splash screen which shows the loading state in a perfectly clear way. Problem is I work for traders, who are always having at least 20 applications running on 6+ screens, I can't keep the splashscreen in a `TopMost` way because it'd disturb their work, and without checking it they launch the application again. I tried adding a NotifyIcon for information, or a small loading progress bar in TopMost, they keep launching it 4 or 5 times. Now it's time to work on it in another way unfortunately :/ – Damascus Jul 04 '11 at 12:51

3 Answers3

3

Your customers' requests can never be stupid - they pay you money.

To bring window to front - create system wide mutex and check its presence on application startup. If it's there - use interprocess communication mechanisms to send message to that other instance to bring its main window to front (a window message or named pipe - both are fine). Here is an example (make sure to check related answers too).

And by any means show splash screen as soon as you can to prevent relaunching application again and again. If it does not appear in 1-2 seconds (2 is too long) it's bad. Responsiveness of your application makes feeling like it works faster.

Community
  • 1
  • 1
Anvaka
  • 15,658
  • 2
  • 47
  • 56
  • Thanks for the link! I omitted it because it seemed perfectly logic, but I do have a splash screen. I'll check on that – Damascus Jul 04 '11 at 12:52
2

Is it something like this you're looking for?

foreach (var p in Process.GetProcesses()) 
{
    if (p.ProcessName.Contains("myProcess"))
    {
        //process is already running
    }
}

Or, with LINQ:

if (Process.GetProcesses().Where(p => p.ProcessName.Contains("myProcess")).Any())
{
    //process is already running
}

If the users complain about startup times, maybe consider checking the version on exit, instead of startup.

Peter
  • 13,733
  • 11
  • 75
  • 122
  • What if you start two instances of the application at the same time, both decide that another is running and both exit? – svick Jul 04 '11 at 11:52
  • True, but that would be an edge case I believe. Nevertheless, you're right. – Peter Jul 04 '11 at 11:54
  • 1
    if we didn't have to handle edge cases, programming would be easy. – svick Jul 04 '11 at 12:02
  • Nice one! I'll try this one. The case brought by svick won't happen in my case, cause users are launching an installer which then calls the actual application, there can't be two instances of the application in memory – Damascus Jul 04 '11 at 12:54
1

I have answered a very similar question yesterday. The only bit that's missing there is how to hide and show the window: use Window.Visibility, set it to Visibility.Hidden to hide the window and to Visibility.Visible to show it again.

Community
  • 1
  • 1
svick
  • 236,525
  • 50
  • 385
  • 514