0

I am trying to process command line arguments for my WPF application for this I have made following changes

I have converted the output type as - Console Application

commented the StartupUri parameter in App.xaml

and override the protected override void OnStartup(StartupEventArgs e) { }

Here is my overridden method

public partial class App : Application
{
    [DllImport("Kernel32.dll")]
    public static extern bool AttachConsole(int processId);

    [DllImport("Kernel32.dll")]
    public static extern bool FreeConsole();

    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        if (e.Args != null && e.Args.Length > 0)
        {
            ExecutionMethod.CommandLine = true;
            ProcessCommandLineArgs.ProcessArgs(e.Args);
            base.Shutdown();
        }
        else
        {
            FreeConsole();
            ExecutionMethod.CommandLine = false;
            TestApp WindowToDisplay = new TestApp();
            WindowToDisplay.Show();
        }
    }
}

I am able to process the arguments and display the result on console.

But the issues is

This TestApp, creates process, say P1 , which execute some commands and result gets re-directed to console output. Before P1 completes, main process exited due to base.Shutdown() in above overridden method.

How I can make this base.shutdown to wait for Process P1 to finish.

Any link/suggestions

Regards

atulya
  • 535
  • 2
  • 8
  • 25
  • Have a look at: http://stackoverflow.com/questions/426421/wpf-command-line. For waiting for the process, you'll need to get a reference to the process and then just wait for it. You might need to post the details of ProcessCommandLineArgs – Ray Jan 07 '15 at 04:56
  • Is [this](http://stackoverflow.com/questions/3147911/wait-till-a-process-ends) and [this](http://stackoverflow.com/questions/6390030/c-sharp-making-a-process-start-wait-until-the-process-has-start-up) is what are you looking for? – Siva Gopal Jan 07 '15 at 05:38
  • thanks Siva and Ray, for reply. – atulya Jan 07 '15 at 09:42

1 Answers1

0

I was actually waiting for new process, P1, to exit.

I synchronized main process and process P1. Making ProceedForShutdwon to true only when process P1 gets exited

while (ExecutionMethod.ProceedForShutDown == false)
{
       mut.WaitOne(500); // wait for 500 ms
}
base.Shutdown();

I am able to achieve my goal.

Hope this will help others also.

atulya
  • 535
  • 2
  • 8
  • 25