Click here to Skip to main content
15,904,935 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi can you help me create a button if clicked it will open the console app and if you click the same button again it will close the console app

Here My code

C#
private void btnStartNStop_Click(object sender, EventArgs e)
        {
            if (true)
            {
                Process p = new Process()
                    {
                        StartInfo = new ProcessStartInfo()
                        {
                            FileName = "cmd.exe",
                            CreateNoWindow = true,
                            UseShellExecute = false,
                            ErrorDialog = false,
                            RedirectStandardInput = true,
                            RedirectStandardOutput = true,
                            RedirectStandardError = true,
                        },
                        EnableRaisingEvents = true,
                        SynchronizingObject = this
                    };

                p.OutputDataReceived += (s, ea) => this.txtOutput.AppendText(ea.Data);

                p.Start();
                p.BeginOutputReadLine(); 
            }
        }
Posted
Comments
Maciej Los 3-Apr-14 14:48pm    
What kind of issue do you have?
Sergey Alexandrovich Kryukov 3-Apr-14 15:01pm    
Right. Not clear, but the code already goes in wrong direction.
I schematically answered, please see Solution 1.
—SA
Sergey Alexandrovich Kryukov 3-Apr-14 14:48pm    
No need to "create a button". All buttons you need already exist. :-)
—SA
Sergey Alexandrovich Kryukov 3-Apr-14 14:49pm    
Do you want to run cmd.exe? Or something else?
—SA
Nico_Travassos 3-Apr-14 14:52pm    
i want one button that starts a console application and if you click the same button again it will close console application the code in button i placed on site is if the but is clicked "Start" then the text of button will change to "Stop" and if you click that it will close the console app

1 solution

The most apparent mistake you is using "CMD.EXE". It's obvious from redirection that you don't need to run it in interactive mode. Hence, you use it to run some other application from this process. This would be more than silly. Instead, you should just start the application you need. "CMD.EXE" is not needed, why? This is just one of applications, nothing special about it.

Now, here is what you need basically:
C#
class SomeClass {

    public SomeClass() {
        // ...
        someButton.Click += (sender, eventArgs) => {
            if (externalProcess == null) {
                externalProcess = System.Diagnostics.Process.Start(
                    fileName,
                    commandLine);
                // change button text to, say, "Stop the Application"
            } else {
                // handle your output data here
                externalProcess.Kill();
                externalProcess = null;
                // change button text to, say, "Start the Application"
            }
        }; //someButton.Click
    } //SomeClass

    System.Diagnostics.Process externalProcess;

    Button someButton = //...
    string fileName = "someApplication.exe"; // whatever it is
    string commandLine = ""some command line parameters""; // whatever it is

} //SomeClass

Very typically SomeClass would be the class of your main form or main WPF window, or something you use for UI.

—SA
 
Share this answer
 
v3
Comments
Maciej Los 3-Apr-14 15:37pm    
Hmmm...
If i understand OP's question correctly, OP wants to open and close "someApplication" using the same button.
What you suggest is to create several buttons with custom event handler. Is that what OP really wants? I don't think so...
Sergey Alexandrovich Kryukov 3-Apr-14 15:41pm    
No, you just missed it. This is exactly what I demonstrated: I take just one button someButton and added a single event handler which creates the process in one case and kills it if is already creating. Additionally, one could check the process status, as well as many other things. (The whole thing is not a really good idea anyway.)

Let's say, the instance of the class shown is a singleton, such as the class for the main form/window.

Well, can you see it now?
—SA
Maciej Los 3-Apr-14 15:51pm    
Forgive me. I'm watching but not see (i hope i translated properly).
+5!
Sergey Alexandrovich Kryukov 3-Apr-14 16:37pm    
Please, no reason to apologize at all; and thank you.
Probably, you mean "look" vs "see", like in "it's not enough to look, you also need to see", etc. Watch is different: "watch TV", "watch this" (I'll show you something), "watch him" (observe the process; often: keep an eye on, be careful, like in "watch out for dog").
—SA
Maciej Los 3-Apr-14 16:58pm    
Thank you Sergey ;)
So, finally how to say: "Я смотрю, но не вижу...": "i'm looking but cannot see"(?) In that case "i'm looking" could means "to find", am i right?
Its English idiom, i know it, but can't find.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900