1

I know that title doesn't explain it well so I'll explain it better here. I'm working on a windows forms application (let it be app A) that essentially is GUI for another written in a different language command-line app (app B). The problem is that I don't know how to run multiple commands on a single instance of an application. I need to make pressing a button change some values with a single command executed on app B and pressing other button displays the values. It doesn't work because after pressing the first button the values change but when another button is pressed another instance of app B is called with displaying command and its values are unchanged (default).

private void button2_Click(object sender, EventArgs e)  //apply command 
        {

            using (Process process = new Process)
            {
                process.StartInfo.FileName = FileLocation;//app B location
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.Arguments = "apply " + textBox1.Text + "#";
                process.Start();

                //apply,# ->command for changing values 
                //textBox1.Text is  input to specify how values are changed

                StreamReader reader = process.StandardOutput;
                string output = reader.ReadToEnd();


                label1.Text = output;

                process.WaitForExit();
            }

        }
private void button8_Click(object sender, EventArgs e)  //view command
        {

            using (Process process = new process)
            {
                process.StartInfo.FileName = FileLocation;
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.Arguments = "view";
                process.Start();
                //view -> command to see values 

                StreamReader reader = process.StandardOutput;
                string output = reader.ReadToEnd();


                label1.Text = output;

                //process.WaitForExit();
             }

        }

I know that the problem is caused by methods using different process but I don't know how to make them use one using block

czechuuu
  • 35
  • 4
  • If I understand you correctly, I think you will need to to subclass `WindowsFormsApplicationBase` in order to create something like a `NewArgumentsReceived` method. **[A VB.NET version might provide some help](https://stackoverflow.com/a/28838693/1070452)** – Ňɏssa Pøngjǣrdenlarp Oct 06 '19 at 19:45
  • You really need to step back and try to describe your problem in a way someone else might be able to understand. Shorter sentences, multiple paragraphs and bullet points, maybe some more punctuation all would help. My mind had a stack overflow trying to parse: *I'm working on a windows forms application (let it be app A) that essentially is GUI for another written in a different language command-line app (app B).* – Flydog57 Oct 06 '19 at 20:13
  • I think the code certainly does what you tell it to do. On each button pressed it creates a new process _(new Process())_ and discards it after execution. If you don't like that behavior just don't do it. You could create the process outside of those functions for instance. I am not sure if I understood the problem. Also I think there are many syntactical errors in the code example – Daniel Schmid Oct 06 '19 at 20:36
  • 1
    [How do I get output from a command to appear in a control on a Form in real-time?](https://stackoverflow.com/a/51682585/7444103). See the notes there. There's also a small Project (the Form in the animation you see there) that you can download, for testing. It's not *exactly* what you're trying to do here, but it's more or less what, in the end, you should do. Read the notes (and keep your Process *alive*). – Jimi Oct 06 '19 at 21:01

0 Answers0