2

I'm currently trying to have a simple CMD function run from my C# WPF program, and display the output in the WPF program. I think I'm close, but its not currently displaying anything. The program will open and have a confirmation that it should run the CMD file. Once 'Yes' is selected, it should open a new frame and run the program, piping it back to the frame. The CMD referenced is a simple netstat cmd. I have changed 'CreateNoWindow' to False and see the CMD opening, so it seems to be executing it.

EDIT: I left out a section of my code, whoops!

EDIT 2: Updated code to include some suggestions. No change. Could it be something to do with how I'm using a Frame?

namespace WpfApp1
{
/// <summary>
/// Interaction logic for Page3.xaml
/// </summary>
    public partial class Page3 : Page
    {
        public Page3()
        {
            InitializeComponent();
        }

        private void Frame_Navigated(object sender, NavigationEventArgs e)
        {
            Task.Run(() => { Muntrainout(); });
        }

        public void Muntrainout()
        {
            System.Diagnostics.Process muntrainout = new System.Diagnostics.Process();
            muntrainout.StartInfo.RedirectStandardOutput = true;
            muntrainout.StartInfo.RedirectStandardError = true;
            muntrainout.StartInfo.RedirectStandardInput = true;
            muntrainout.StartInfo.UseShellExecute = false;
            muntrainout.StartInfo.CreateNoWindow = true;
            muntrainout.StartInfo.FileName = @"C:\Users\user.name\Documents\Project\Test.cmd"; 
            muntrainout.OutputDataReceived += new System.Diagnostics.DataReceivedEventHandler((sender, e) =>
                 {
                     Console.WriteLine(e.Data);
                 }
            );
            // Error Handling
            muntrainout.ErrorDataReceived += new System.Diagnostics.DataReceivedEventHandler((sender, e) => { Console.WriteLine(e.Data); });
            muntrainout.Start();
            muntrainout.BeginOutputReadLine();
            muntrainout.WaitForExit();
        }
    }
}
Oxyauna
  • 75
  • 2
  • 9
  • 1
    Look at the Process.BeginOutputReadLine() article at MSDN for good sample code. It will remind you to write an event handler for the OutputDataReceived event. Task.Run() prevents you from seeing exceptions, favor the Exited event instead. – Hans Passant Sep 19 '18 at 13:08
  • @HansPassant I seem to have missed placing that portion into my code. Very new to c#, not sure if this is what you mean. Thank you for the input! – Oxyauna Sep 19 '18 at 13:25
  • Try moving the muntrainout.OutputDataReceived before the Start(). Other examples I've seen have that set before starting the process. – KornMuffin Sep 19 '18 at 16:27
  • Also take a look at ... https://stackoverflow.com/a/8809005/512365 – KornMuffin Sep 19 '18 at 16:28
  • @KornMuffin I actually looked at that thread before this and followed your idea about moving the 'Start();'. No change unfortunately. There must be something simple I'm missing, I just can't place it! – Oxyauna Sep 19 '18 at 19:06
  • @Oxyauna - Hans offers some good advice regarding looking into the Process.Exited event as an alternative to firing off a Task. Do you see the output if you remove executing in the Task? – KornMuffin Sep 20 '18 at 03:05

1 Answers1

1

Assuming you want to display the output in your frame...

this works for me:

muntrainout.OutputDataReceived += new System.Diagnostics.DataReceivedEventHandler((sender, e) =>
            {
                //Console.WriteLine(e.Data);
                Frame1.Dispatcher.Invoke(() => { Frame1.Content += e.Data+Environment.NewLine; });

            }

It looks a bit ugly but it displays the output... I don't know how Console.WriteLine() should write to your frame?

Marco Rebsamen
  • 605
  • 7
  • 30
  • This is exactly what I'm looking for, I just didn't know how to implement it. It seems that the Dispatcher is not working correctly though, 'object' does not contain a definition for 'Dispatcher'. Is there something I'm missing? Thanks for the input! – Oxyauna Sep 20 '18 at 12:36
  • It depends... `Frame1` is the name I gave my frame control on the form. But there are other ways to get the dispatcher, check this post https://stackoverflow.com/questions/11625208/accessing-ui-main-thread-safely-in-wpf – Marco Rebsamen Sep 20 '18 at 12:59
  • Got it! For some reason the x:name of Frame1 was an issue. Changing it to Frame2 seemed to work fine. Thanks for your help! – Oxyauna Sep 20 '18 at 13:48