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

            // EXECUTE APP This is working code

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)

                    //if the file size has not changed, idle

                    if (reader.BaseStream.Length == lastMaxOffset)

                    //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;

5 Years
Discussion Span
Last Post by skatamatic

the problem is in while(true) loop. It seems it takes to infinity (it does not end, becuase you never call false).


I dont even know why do you have that while loop there, personally I thinks its pointless, so throw it out of the code. What your code does, its only reading some file. So when it reads, I can go out of this method. Or you want to read it constantly?
If so, you have to create a new thread, and put the code onto it, otherwise the main thread (UI thread) will become inactive (unresponsible).

But reading can be done by avoiding a thread, by just creating a Timer class, which can fires up every some seconds (set Interval property). In a Tick event call gr_log_tail() method.

So, this is mostly it. The While(true) is the problem now.


Thank you for the replys.
Ideally when I call the runApp() command it will do some processing. Once that is completed there is no need to read the log anymore.

Ill make the changes to the while loop. I found the code online for the tail reader, so its not something i built.


This might be lending any help to your problem...but why is there a Thread.Sleep() in that loop? It seems quite literally like a waste of time.

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.