Hi All,
Im trying to write an app where im running a command from command prompt (which works perfectly) and it writes to a log file. Now I would like to have a text box tail the log file while the command is being processed. Since adding the tail code in the app is no longer working and just hangs.
The first thing I do in the tail portion is clear the textboxes contents but this isn't even happening. So i'm thinking something is wrong there.
Also I'm wondering if i designed my code correctly to even execute the command and tail at the same time.
private void runApp_Click(object sender, EventArgs e)
{
// CLEAR App output
outputTextbox.Text = "";
// Load log file into tail and begin tail
gr_log_tail();
// EXECUTE APP This is working code
run_app();
}
Here is the tail portion of the code:
private void gr_log_tail()
{
string fileName ="C:\\APP\\log\\app.log";
// Clear contents of tail for each load.
gr_log.Text = "";
// Tail app.log file during load.
using (StreamReader reader = new StreamReader(new FileStream(fileName,
FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
{
//start at the end of the file
long lastMaxOffset = reader.BaseStream.Length;
while (true)
{
System.Threading.Thread.Sleep(100);
//if the file size has not changed, idle
if (reader.BaseStream.Length == lastMaxOffset)
continue;
//seek to the last max offset
reader.BaseStream.Seek(lastMaxOffset, SeekOrigin.Begin);
//read out of the file until the EOF
string line = "";
while ((line = reader.ReadLine()) != null)
gr_log.Text += line;
//update the last max offset
lastMaxOffset = reader.BaseStream.Position;
}
}
}