0

I have this small piece of code (easy to try):

class Program
{
    static void Main(string[] args)
    {
        List<string> paths = new List<string>() { "1.docx", "2.docx", "3.docx", "4.docx" };

        foreach (string path in paths)
        {
            string path2 = @"Path\to\those\files" + path;

            Process process = new Process();
            process.StartInfo = new ProcessStartInfo(path2);
            System.Diagnostics.Debug.WriteLine("~>" + path2);
            process.Exited += (s1, e1) => process_Exited(path2);
            process.EnableRaisingEvents = true;
            process.Start();
        }

        while (true) { }
    }

    static void process_Exited(string path)
    {//<~ breakpoint here

    }
}

What is happening is sometimes, the breakpoint is hit moments after I start the application despite it having to wait for the processes to be exited one after another. It is always the last file of all, be it 2 files, 3 files, 4 files or more. The only time the breakpoint is never hit prematurely is when paths contains only one file. (By the way, maybe I could not care much about this strange behaviour, but when I really exit the .docx file (the last one from the paths list), the breakpoint is not hit.

Why is this (process sometimes exiting prematurely) happening and how to prevent that?

UPDATE: I just noticed it is not necessarily the last of files in paths, sometimes it is random one.

Andrius Naruševičius
  • 8,348
  • 7
  • 49
  • 78
  • 1
    What happens when using [WaitForExit](http://msdn.microsoft.com/en-us/library/fb4aw7b8.aspx) (for *each* process)? – Paul Jul 05 '13 at 06:36
  • If I understood correctly, I had to put that line just after `process.Start()`? Then the next process didn't start until the previous one was closed by me. It looked like: `Start. Manual close by me. Start. Manual close by me...` No premature exists though this way. Maybe I should get a new thread for each process? – Andrius Naruševičius Jul 05 '13 at 06:43
  • Well, wait for them all at some point. Make a list of processes, start them, then loop over the list (after creating them all) and wait for each one to stop (it should take roughly the time of the longest process). Then see if this makes the observed behavior any different. Inspect the error codes to make sure they are "success". As far the breakpoint goes, I think that might be hard to see what's going on - I'm not sure which thread it's called on or if it will be called synchronously wrt other exited events or not. – Paul Jul 05 '13 at 06:50
  • Unfortunately, one of them (never more than 1, but in rare occasions - none) still exits. Even more, the loop doesn't go fully, but only loops once and waits. I tried putting each of `WaitForExit` into a different thread. This way, firstly, all executed the method `WaitForExit` and only then, after the full loop was over, one process exited. Maybe this tells something to you. – Andrius Naruševičius Jul 05 '13 at 07:00
  • Yes, I already have removed the breakpoint and replaced it with writing a message to the output. The exit code for the premature one is `0` as well as is for others once I close them manually. – Andrius Naruševičius Jul 05 '13 at 07:07
  • Did you test this with real processes instead of Word files? I assume that the problem here has something to do with the fact that Word opens multiple files in the same process. Maybe it briefly starts a second process, sees that Word is already running and therefore exits the second process again? – cremor Jul 05 '13 at 07:16
  • Did I mention, that the `.docx` file remains open even though the premature exit happens? I think I probably missed to tell this important part. @cremor that is an interesting thought! But unfortunately what I need is to open many of these word files in the final application. Maybe I should add some delay? Also, as I said, it only exits ONE of these files, never happened with more that one. Shouldn't it happen with 3 files instead? – Andrius Naruševičius Jul 05 '13 at 07:18
  • @cremor Yes, I tested this with files of different format and the premature exit is gone. Now the only quest remaining is how to make word not exit the process. – Andrius Naruševičius Jul 05 '13 at 07:31

1 Answers1

0

Eventually, unable to solve the primary issue, I ended up using NetOffice:

NetOffice.WordApi.Application wordApp = new NetOffice.WordApi.Application();
wordApp.Visible = true;
NetOffice.WordApi.Document doc = wordApp.Documents.Open(path);

which solved the premature closing.

Andrius Naruševičius
  • 8,348
  • 7
  • 49
  • 78