Process waitFor command

Discussion in 'Java' started by Matthew Zimmer, Jan 6, 2004.

  1. Hey all,
    I'm working on a program where I open up a word document using the exec
    command like this:
    Process p = r.exec(...); // this works fine

    I then want to receive notification from Windows when word is done with
    the file. I can kind of do that by using the waitFor method on Process
    like this:
    int res = p.waitFor();

    This basically worked in most cases. As soon as I shut down word, my
    program resumes after the waitFor command. However, if I open up the
    word document at a time when another file is open in word something
    weird happens. It appears that windows creates a new instance of word,
    then opens the file in another instance, then shuts down the original
    instance which is the one that waitFor is waiting for. Thus, even
    though the file is open, the original process closes which means that I
    get to the next line in my code well before it should be there.

    Does anybody have any idea how to stop this? I've tried using the whole
    exclusive open in java io, but that doesn't work at all. Thanks for any
    help.
    Matthew
    Matthew Zimmer, Jan 6, 2004
    #1
    1. Advertising

  2. Matthew Zimmer wrote:

    > Hey all,
    > I'm working on a program where I open up a word document using the exec
    > command like this:
    > Process p = r.exec(...); // this works fine
    >
    > I then want to receive notification from Windows when word is done with
    > the file. I can kind of do that by using the waitFor method on Process
    > like this:
    > int res = p.waitFor();
    >
    > This basically worked in most cases. As soon as I shut down word, my
    > program resumes after the waitFor command. However, if I open up the
    > word document at a time when another file is open in word something
    > weird happens. It appears that windows creates a new instance of word,
    > then opens the file in another instance, then shuts down the original
    > instance which is the one that waitFor is waiting for. Thus, even
    > though the file is open, the original process closes which means that I
    > get to the next line in my code well before it should be there.
    >
    > Does anybody have any idea how to stop this? I've tried using the whole
    > exclusive open in java io, but that doesn't work at all. Thanks for any
    > help.
    > Matthew


    Quite a lot of apps work like this, and it can be annoying. However, there
    is usually an option you can set to force it to open in a new instance.

    I can't test any of them as I don't have Word, but there are some switches
    listed in http://support.microsoft.com/default.aspx?scid=kb;EN-US;210565
    that may help you.

    --
    Neil Campbell
    batneil[AT]thebatcave[DOT]org[DOT]uk
    http://www.thebatcave.org.uk
    Neil Campbell, Jan 6, 2004
    #2
    1. Advertising

  3. Neil Campbell wrote:

    > Matthew Zimmer wrote:
    >
    >
    >>Hey all,
    >>I'm working on a program where I open up a word document using the exec
    >>command like this:
    >>Process p = r.exec(...); // this works fine
    >>
    >>I then want to receive notification from Windows when word is done with
    >>the file. I can kind of do that by using the waitFor method on Process
    >>like this:
    >> int res = p.waitFor();
    >>
    >>This basically worked in most cases. As soon as I shut down word, my
    >>program resumes after the waitFor command. However, if I open up the
    >>word document at a time when another file is open in word something
    >>weird happens. It appears that windows creates a new instance of word,
    >>then opens the file in another instance, then shuts down the original
    >>instance which is the one that waitFor is waiting for. Thus, even
    >>though the file is open, the original process closes which means that I
    >>get to the next line in my code well before it should be there.
    >>
    >>Does anybody have any idea how to stop this? I've tried using the whole
    >>exclusive open in java io, but that doesn't work at all. Thanks for any
    >>help.
    >>Matthew

    >
    >
    > Quite a lot of apps work like this, and it can be annoying. However, there
    > is usually an option you can set to force it to open in a new instance.
    >
    > I can't test any of them as I don't have Word, but there are some switches
    > listed in http://support.microsoft.com/default.aspx?scid=kb;EN-US;210565
    > that may help you.
    >


    Thanks. It turns out that using "/n" which "opens up a new instance of
    word with no document" does NOT work, but using "/w" which "opens up a
    new instance of word with a blank document" DOES work. I have no clue
    why one works and the other doesn't, but that's MS for you. :) Of
    course, if anybody knows how I can make the waitFor work for anything
    I'd appreciate any other thoughts, but thanks again for helping me with
    this one.
    Matthew
    Matthew Zimmer, Jan 6, 2004
    #3
  4. Matthew Zimmer

    X_AWemner_X Guest

    > >>I'm working on a program where I open up a word document using the exec
    > >>command like this:
    > >>Process p = r.exec(...); // this works fine
    > >>I then want to receive notification from Windows when word is done with
    > >>the file. I can kind of do that by using the waitFor method on Process
    > >>like this:
    > >> int res = p.waitFor();
    > >>This basically worked in most cases. As soon as I shut down word, my
    > >>program resumes after the waitFor command. However, if I open up the
    > >>word document at a time when another file is open in word something
    > >>weird happens. It appears that windows creates a new instance of word,
    > >>then opens the file in another instance, then shuts down the original
    > >>instance which is the one that waitFor is waiting for. Thus, even
    > >>though the file is open, the original process closes which means that I
    > >>get to the next line in my code well before it should be there.
    > >>
    > >>Does anybody have any idea how to stop this? I've tried using the whole
    > >>exclusive open in java io, but that doesn't work at all. Thanks for any
    > >>help.


    > > Quite a lot of apps work like this, and it can be annoying. However,

    there
    > > is usually an option you can set to force it to open in a new instance.
    > > I can't test any of them as I don't have Word, but there are some

    switches
    > > listed in http://support.microsoft.com/default.aspx?scid=kb;EN-US;210565
    > > that may help you.


    > Thanks. It turns out that using "/n" which "opens up a new instance of
    > word with no document" does NOT work, but using "/w" which "opens up a
    > new instance of word with a blank document" DOES work. I have no clue
    > why one works and the other doesn't, but that's MS for you. :) Of
    > course, if anybody knows how I can make the waitFor work for anything
    > I'd appreciate any other thoughts, but thanks again for helping me with
    > this one.


    Most likely will need a native wrapper to handle windows processes. You
    basicly launch "myprocessrunnerwrapper.exe <params>" and wait for it to
    exit. If word really opens up a new process with different PID then it needs
    a tricks to overcome the problem. Probably Excel does same.
    X_AWemner_X, Jan 7, 2004
    #4
  5. X_AWemner_X wrote:

    >>>>I'm working on a program where I open up a word document using the exec
    >>>>command like this:
    >>>>Process p = r.exec(...); // this works fine
    >>>>I then want to receive notification from Windows when word is done with
    >>>>the file. I can kind of do that by using the waitFor method on Process
    >>>>like this:
    >>>> int res = p.waitFor();
    >>>>This basically worked in most cases. As soon as I shut down word, my
    >>>>program resumes after the waitFor command. However, if I open up the
    >>>>word document at a time when another file is open in word something
    >>>>weird happens. It appears that windows creates a new instance of word,
    >>>>then opens the file in another instance, then shuts down the original
    >>>>instance which is the one that waitFor is waiting for. Thus, even
    >>>>though the file is open, the original process closes which means that I
    >>>>get to the next line in my code well before it should be there.
    >>>>
    >>>>Does anybody have any idea how to stop this? I've tried using the whole
    >>>>exclusive open in java io, but that doesn't work at all. Thanks for any
    >>>>help.

    >
    >
    >>>Quite a lot of apps work like this, and it can be annoying. However,

    >
    > there
    >
    >>>is usually an option you can set to force it to open in a new instance.
    >>>I can't test any of them as I don't have Word, but there are some

    >
    > switches
    >
    >>>listed in http://support.microsoft.com/default.aspx?scid=kb;EN-US;210565
    >>>that may help you.

    >
    >
    >>Thanks. It turns out that using "/n" which "opens up a new instance of
    >>word with no document" does NOT work, but using "/w" which "opens up a
    >>new instance of word with a blank document" DOES work. I have no clue
    >>why one works and the other doesn't, but that's MS for you. :) Of
    >>course, if anybody knows how I can make the waitFor work for anything
    >>I'd appreciate any other thoughts, but thanks again for helping me with
    >>this one.

    >
    >
    > Most likely will need a native wrapper to handle windows processes. You
    > basicly launch "myprocessrunnerwrapper.exe <params>" and wait for it to
    > exit. If word really opens up a new process with different PID then it needs
    > a tricks to overcome the problem. Probably Excel does same.
    >


    I think you're right on this one. I'll probably end up having to do
    exactly that, but was really hoping to be able to avoid it. Thanks.
    Matthew Zimmer, Jan 7, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. googlepost
    Replies:
    0
    Views:
    1,558
    googlepost
    Jul 21, 2003
  2. Replies:
    6
    Views:
    4,718
  3. Kevin
    Replies:
    2
    Views:
    24,541
    Kevin
    Mar 31, 2005
  4. Replies:
    2
    Views:
    787
  5. Michael Grove
    Replies:
    5
    Views:
    2,120
    Michael Grove
    Aug 23, 2005
Loading...

Share This Page