Ehud Grand
Ehud Grand

Reputation: 3693

Process.Start() and continue with my code

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.

Upvotes: 0

Views: 2129

Answers (2)

Justin Harvey
Justin Harvey

Reputation: 14672

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.

Upvotes: 1

Mx.
Mx.

Reputation: 3665

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.

Upvotes: 1

Related Questions