Why i am not getting complete output while executing time consuming commands

Nov 13, 2013 at 6:21 AM
hi

I am trying to execute some 20 commands of which some takes 5 seconds and some of them might take 5 min. So please let me know what is best approach to get the complete output


private void button1_Click(object sender, EventArgs e)
    {
        using (var client = new SshClient(txtIP.Text.ToString().Trim(), txtUser.Text.ToString().Trim(),txtPassword.Text.ToString().Trim()))
        {
            client.Connect();
            using (var stream = client.CreateShellStream("dumb", 80, 24, 800, 600, 1024))
            {
                var reader = new StreamReader(stream);
                var writer = new StreamWriter(stream);
                writer.AutoFlush = true;
                while (stream.Length == 0)
                {
                    Thread.Sleep(500);
                }
                ReadStream(reader);
                WriteStream(txtCommand.Text.ToString(), writer, stream);
                ReadStream(reader);
            }
            client.Disconnect();
        }
    }
    private void ReadStream(StreamReader reader)
    {
        string result="";
        string line = reader.ReadLine();
        while (line != null)
        {
            line = reader.ReadLine();
            result = result + "\r\n" + line;
        }

    }

    private void WriteStream(string cmd, StreamWriter writer, ShellStream stream)
    {
        writer.WriteLine(cmd);
        while (stream.Length == 0)
        {
            Thread.Sleep(500);
        }
    }