简体   繁体   中英

c# Process start continue

i am trying the below code

Process p = new Process();
        ProcessStartInfo info = new ProcessStartInfo();
        info.FileName = "cmd.exe";
        info.RedirectStandardInput = true;
        info.UseShellExecute = false;

        p.StartInfo = info;
        p.Start();

        using (StreamWriter sw = new StreamWriter(p.StandardInput.BaseStream))
        {
            if (sw.BaseStream.CanWrite)
            {                    
                sw.WriteLine(@"dir");                    
            }
        }

        using (StreamWriter sw = new StreamWriter(p.StandardInput.BaseStream))
        {
            if (sw.BaseStream.CanWrite)
            {                 
                sw.WriteLine(@"dir");
            }
        }

the first sw.writeline is going ok, but the second i discovered that the process hasexeited is true.

why the process exited , and how to keep it running to execute further commands?

Put both write commands in the same using block and omit the second using block. The using-block disposes the StreamWriter after you leave the block. Disposing the StreamWriter closes the underlying stream and causes cmd to exit.

i solved the problem like that

static Queue<string> CmdQueue = new Queue<string>();
    static Thread ThreadCMD = new Thread(new ThreadStart(CMDThread));

    private static void CMDThread()
    {
        bool Active = true;         // used to indicate the status of the session 

        ProcessStartInfo psiOpt = new ProcessStartInfo();

        psiOpt.FileName = "cmd.exe";
        psiOpt.WindowStyle = ProcessWindowStyle.Hidden;
        psiOpt.RedirectStandardOutput = true;
        psiOpt.RedirectStandardInput = true;
        psiOpt.RedirectStandardError = true;
        psiOpt.UseShellExecute = false;
        psiOpt.CreateNoWindow = true;


        Process procCommand = Process.Start(psiOpt);
        procCommand.OutputDataReceived += ProcCommand_OutputDataReceived;
        procCommand.ErrorDataReceived += ProcCommand_ErrorDataReceived;
        procCommand.BeginOutputReadLine();
        procCommand.BeginErrorReadLine();


        using (StreamWriter sw = procCommand.StandardInput)
        {
            while (Active)
            {
                while (CmdQueue.Count==0) { }
                string cmd = CmdQueue.Dequeue();
                if (cmd != "cmdexit")
                {                        
                    sw.WriteLine(cmd);                        
                }
                else
                {
                    Active = false;
                }
            }
        }
        procCommand.OutputDataReceived -= ProcCommand_OutputDataReceived;
        procCommand.ErrorDataReceived -= ProcCommand_ErrorDataReceived;
        procCommand.Close();
    }

    private static void ProcCommand_ErrorDataReceived(object sender, DataReceivedEventArgs e)
    {
        PushCMDResult(Encoding.ASCII.GetBytes(e.Data));
    }

    private static void ProcCommand_OutputDataReceived(object sender, DataReceivedEventArgs e)
    {
        PushCMDResult(Encoding.ASCII.GetBytes(e.Data));
    }

    public void StartCMD()
    {
        ThreadCMD.Start();
    }

    public void Execute(string cmd)
    {
        CmdQueue.Enqueue(cmd);
    }

i can start cmd.exe session , and execute as many as needed of commands using execute to enqueue the cmd string in Queue which will be written to the streamwriter . i used async reading for output and error as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM