0

I am trying to prevent opening help file more than once. This is the method I am using:

    public void openHelp()
    {
        int count = 0;
        string helpPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + @"\MyApp\Help\eHelp.chm";

        System.Diagnostics.Process[] helpProcs = System.Diagnostics.Process.GetProcesses();
        foreach (System.Diagnostics.Process proc in helpProcs)
        {
            if (proc.MainWindowTitle == "Sample App Help")
            {
                count++;
            }
        }

        if (count == 0)
        {
            System.Diagnostics.Process.Start(helpPath);
        }
        else
        {
        }
    }

The idea is, if you find the process with the same MainWindowTitle, then do not start a new one. However, this is not reliable. In some cases it still starts the process, even though one is already running. Is there an issue with a logic?

Thank you.

P.S. Of course the MainWindowTitle is "Sample App Help", at least that is what I see while debugging.

Update: Issue only occurs when user has minimised help file. So I suspect something happens in the system and I need to check something. Any suggestions?

Alex
  • 937
  • 3
  • 20
  • 44
  • Are you referring to a CHM help file? – rro Dec 07 '12 at 10:24
  • Yep, exactly what I am doing. Of course, I cannot filter out by process name "hh" (appears for help file in processes) , because a user may be viewing different help file while I'm trying to open my file. – Alex Dec 07 '12 at 10:26
  • Try `if(proc.MainModule.FileName == helpPath)` instead. – gaynorvader Dec 07 '12 at 10:34
  • I believe a `Mutex` can be used to signal between processes? – Phil Gan Dec 07 '12 at 10:34
  • Have you tried looking at the minimized window with Spy++? – Zach Johnson Dec 07 '12 at 10:34
  • @PhilGan that works if you own the code, but I think Alex is seeing a problem with non-owned code (the CHM help viewer.) – Zach Johnson Dec 07 '12 at 10:36
  • @ZachJohnson Oh I see. I'm wondering if it's appropriate to stop the user from opening more than one help file at once; sometimes you want to have multiple windows open so you can check things as you work. – Phil Gan Dec 07 '12 at 10:41
  • @gaynorvader interesting point, however causes an exception in my case, since help file viewer is 64-bit app, and my application is x86. – Alex Dec 07 '12 at 10:49
  • @PhilGan I thought about bringing the help file on foreground, just like Office and other MS applications tend to do. – Alex Dec 07 '12 at 10:50

1 Answers1

4

The Remarks section in Process.MainWindowTitle contains the following note:

The main window is the window that currently has the focus; note that this might not be the primary window for the process. You must use the Refresh method to refresh the Process object to get the current main window handle if it has changed.

Could this perhaps be the cause of your problem?

What about keeping the process id of a newly started help viewer and before starting another one, just check if the old one is still alive.

int id = ...

try
{
    var proc = Process.GetProcessById(id);
}
catch
{
    // no process running with that id
}
Clemens
  • 123,504
  • 12
  • 155
  • 268