0

My program is in win forms (c#). It should open an external program, do a printscreen of it's main window, and close it. By using Process.Start() I can open the the program, but then all the focus is on it and my code is halted. Only when I close it myself my form continues- but it's too late for the screenshot. So how do I force my code to keep running?

    public void Runttk(string maromnum)
    {
        Process runttk = new Process();
        runttk.StartInfo.UseShellExecute = true;               
        runttk.StartInfo.FileName = @"C:\\program.exe";
        runttk.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
        runttk.Start();
        this.Focus();
        try
        {
            if (runttk.WaitForInputIdle()==true)
            {                                          
                PringJpg(maromnum);
                Killttk();
            }
        }
        catch (Exception e)
        {
        }

    }

Thank you

UPDATE: eventuanlly I've used Thread.Sleep(3000). Crued but do the trick. I didn't used backgroundworker because the sync between the finale uplaod the the extenral program and my code wasn't clear enough.

Ehud Grand
  • 3,501
  • 4
  • 33
  • 52
  • Process.Start() WILL return control to your program immediately. Run it under the debugger or add some Debug.WriteLine() to see where your code is stopping. Perhaps WaitForInputIdle() is not returning until the external program exits? – Matthew Watson Aug 09 '12 at 10:16
  • Related: http://stackoverflow.com/questions/2740038/how-do-i-wait-until-a-console-application-is-idle So use a synchronization object across processes for that. – Sebastian Graf Aug 09 '12 at 10:19
  • @Duane do you know a good example for threading? I'm new to it. – Ehud Grand Aug 09 '12 at 10:55
  • BackgroundWorker is pretty straight forward and easy to use, are you using winforms though? – dtsg Aug 09 '12 at 11:02

2 Answers2

1

Maybe your programm is never idle - means that runttk.WaitForInputIdle()==true let your app wait until you close it.

Add a limit ( for example runttk.WaitForInputIdle(500)==true) should fulfill your needings.

Mx.
  • 3,588
  • 2
  • 25
  • 33
  • @Sebastian, this check is being made against the running process which is a win forms app, not a console window. – Justin Harvey Aug 09 '12 at 10:22
  • darn.. did a ninja delete. What I wrote was essentially: Console windows have no (window) UI, therefore they will never enter an InputIdle state and the call to `runttk.WaitForInputIdle()` is blocking. – Sebastian Graf Aug 09 '12 at 10:23
  • 2
    @Sebastian: In any case, WaitForInputIdle() will always return IMMEDIATELY if it is a console app. (This is documented here: http://msdn.microsoft.com/en-us/library/windows/desktop/ms687022%28v=vs.85%29.aspx) – Matthew Watson Aug 09 '12 at 10:32
  • Hey it's not console. @Sebastian adding a parameter is problematic becuase I don't know how much time would take for the program to start. When I enter 5000 it prints it in the middle of starting. – Ehud Grand Aug 09 '12 at 10:38
1

Trying your code, but with another program like notepad.exe, Notepad runs and then control drops through to where you call PringJpg.

So I think the problem is that it is blocking on if (runttk.WaitForInputIdle()==true), please try adding a timeout to this operation.

Justin Harvey
  • 14,446
  • 2
  • 27
  • 30