Nazima
Nazima

Reputation: 291

How do I limit my Windows application to spawning a single instance of a process?

I am working on a Windows application. I have created a help file (.chm) using a third party tool and I call it from the Windows application. The help file is invoked successfully when I click on Help menu item or press F1 for the application.

The problem I am facing is that if I click on the menu item again or press F1 (while the application is open and I have not closed the previous help file) it opens another instance of the help file.

Let me be clear by giving an example: I want it to be just like the "Solitaire" game. No matter how many times you press F1 or the Contents menu item it shows only one help window.

I want to do same thing like "Solitaire". I don't want multiple instances to be created.

I hope you understood my problem. Please let me know if my query is wrong. I have posted my code below.

    ProcessStartInfo info;
    Process exeprocess;

The below code is in Help menuitem click event.

    private void mnuContents_Click(object sender, EventArgs e)
     {
         string ApplicationPath=ConfigurationSettings.AppSettings["HelpFile"].ToString();
         info = new ProcessStartInfo(ApplicationPath);
         //Process.Start(info);
         exeprocess = Process.Start(info);
     }

Upvotes: 2

Views: 1029

Answers (3)

Gabriel Magana
Gabriel Magana

Reputation: 4536

Ok this is your block of code to start the CHM viewer:

 private void mnuContents_Click(object sender, EventArgs e)
 {
     string ApplicationPath=ConfigurationSettings.AppSettings["HelpFile"].ToString();
     info = new ProcessStartInfo(ApplicationPath);
     //Process.Start(info);
     exeprocess = Process.Start(info);
 }

in exeprocess there is a property called Id. You need to keep track of that Id for the next time the user presses F1 or the menu key.

You need to do a check like

 private void mnuContents_Click(object sender, EventArgs e)
 {
     if (Process.GetProcessById(self.previousId) != null) {
         string ApplicationPath=ConfigurationSettings.AppSettings["HelpFile"].ToString();
         info = new ProcessStartInfo(ApplicationPath);
         //Process.Start(info);
         exeprocess = Process.Start(info);
         self.previousId = exeprocess.Id;
     }
 }

Something like that would work. If you want to get fancy, you can bring the already-running process to the foreground as well.

Upvotes: 0

bobbymcr
bobbymcr

Reputation: 24177

You have a Process object, so you should probably store it somewhere and check if it is still active the next time the help command is invoked. You could use Process.HasExited for that purpose. If it has exited, clean up the Process object by calling Dispose() and then launch a new instance, storing it away again. Repeat as needed.

Upvotes: 1

paulsm4
paulsm4

Reputation: 121881

One solution is:

  1. Have your application create a system-wide resource (the example below uses a Win32 mutex)

  2. Check the resource before you spawn the .chm (I imagine you're probably using ShellExec or some variant to spawn the help file.

Here's example code (in C++/Win32 code):

http://support.microsoft.com/kb/243953

Another, different approach is to see if any currently running processes match the one you would spawn. Here's example code for this approach:

http://www.dotnetperls.com/single-instance-windows-form

Upvotes: 1

Related Questions