We have a requirement to initiate more than one instance of an
application using the filenames. (the example below will start two
instances of MS Word).
The problem is that we need to close each word document individually,
but this does not appear possible using the Process object. When I
run the example below the process object "p" can be viewed using Quick
Watch however process object p2 is displayed as undefined. This
appears to be because the second word document is opened under the
same Word process as the first word document. So when p.kill() is
called it kills off both word documents (not what we want).
-Code----------------------------------------------
ProcessStartInfo startInfo = new ProcessStartInfo("a.doc");
startInfo.WindowStyle = ProcessWindowStyle.Minimized;
Process p = Process.Start(startInfo);
.....
ProcessStartInfo startInfo2 = new ProcessStartInfo("b.doc");
startInfo2.WindowStyle = ProcessWindowStyle.Minimized;
Process p2 = Process.Start(startInfo2);
...
p.Kill();
...
p2.Kill();
-End Code----------------------------------------------
This works ok when I try it with text files since windows opens two
instances of notepad.
I have a way around it using the InteropServices (User32.dll) to find
the window handles and then issuing a windows message i.e.
int iHandle1 = FindWindow("OpusApp", "a.doc - Microsoft Word");
int iHandle2 = FindWindow("OpusApp", "b.doc - Microsoft Word");
int ret1 = SendMessage(iHandle1, WM_CLOSE, 0x00000000, 0x00000000);
int ret2 = SendMessage(iHandle2, WM_CLOSE, 0x00000000, 0x00000000);
However this has at least two drawbacks.
1. Have to use the 32 bit windows api calls via InteropServices (not
ideal)
2. For the FindWindow call we have to cater specifically for word
documents by passing the class name ("OpusApp") and the additional
text " - Microsoft Word" in the title of the window.
Has anyone come across this issue before or do you have any
suggestions.
Regards
Gavin