Reputation: 25
I am working on a C# program that implements the SSH.Net library. One of the functions of the program allows for a command to be sent over SSH to the target server and then display the output in a text field.
This works fine, but I am running into an issue when the output response is large. Currently all the output is displayed until complete. I need a way to , for example show 30 lines, then wait for user input, the show the next 30 lines.
I can easily stop the output at 30 lines with a for loop and a counter, but I am unsure how to start it up again, how to I get back to the same point in the streamreader ?
var list = new List<string>();
string line;
output_textBox.Text = String.Empty;
while (!asynch.IsCompleted)
{
using (StreamReader sr = new StreamReader(cmd.OutputStream))
{
while ((line = sr.ReadLine()) != null)
{
list.Add(line);
Console.WriteLine(line);
}
}
}
Thanks
EDIT
Got it working with the below.
using (StreamReader sr = new StreamReader(cmd.OutputStream))
{
while (!sr.EndOfStream)
{
while (line_count < 100 && (line = sr.ReadLine()) != null)
{
Console.SetOut(new TextBoxWriter(output_textBox));
Console.WriteLine(line);
line_count++;
}
MessageBox.Show("OK to continue");
line_count = 0;
}
Upvotes: 1
Views: 823
Reputation: 8286
To get back to the line where you finished last time:
int startFrom = 30; // skip first 30 lines
using (StreamReader rdr = new StreamReader(fs))
{
// skip lines
for (int i = 0; i < startFrom ; i++) {
rdr.ReadLine();
}
// ... continue with processing file
}
UPDATE
public void Process() {
//init
int startFrom = 0;
int stepCount = 100;
//read data 0 - 100
ReadLines(startFrom, stepCount);
startFrom += stepCount;
// after user action
//read data 100 - 200
ReadLines(startFrom, stepCount);
}
public void ReadLines( int skipFirstNum, int readNum ) {
using (StreamReader rdr = new StreamReader(cmd.OutputStream)) {
// skip lines
for (int i = 0; i < skipFirstNum; i++) {
rdr.ReadLine();
}
for (int i = 0; i < readNum ; i++) {
// ... these are the lines to process
}
}
}
Upvotes: 1
Reputation: 2182
It seems you are using parallel programming. You can write two functions as Producer & Consumer. For example a producer will continuously read your text and put it in in-memory list, and the consumer will consumed (and remove consumed lines) from the list at your appropriate time interval.
Upvotes: 1