Question

What is the code to call a console application within a WPF? I have an application in WPF you need:

  1. call a console application;
  2. the console application closes the WPF
  3. The console application again calls WPF and close console application.

This is necessary because I am doing a system where updates to the application must close the files to copy.

For close application WPF I am using the following:

Process wpfProc = Process.GetProcessesByName("MainWindow.exe").First();
wpfProc.Kill();

return in console application:

Unhandled Exception: System.InvalidOperationException: Sequence sontains no elements
    at System.Link.Enumerable.First[TSource]<IEnumerable'1 source>
    at Updater.Program.Main<String[] args> in d:\endereçodoUpdater\Program.cs:line 17

in line 17 have the following:

Process wpfProc = Process.GetProcessesByName("MainWindow").First();

How do I resolve this?

Was it helpful?

Solution

You do this with Process.Start:

Process myProc = Process.Start("MyConsoleApp.exe");

//Close gracefully
Application.Exit();

In MyConsoleApp.exe, you would need to use GetProcessByName to kill your WPF app, and then Process.Start again to restart it:

Process wpfProc = Process.GetProcessesByName("MyWpfApp.exe").First();

//If you want to directly kill it
wpfProc.Kill();

//Or be nice and let it kill itself
wpfProc.WaitForExit();

//Do stuff
Process.Start("MyWpfApp.exe");

System.Diagnostics.Process on MSDN

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top