0

I am going through this tutorial on how to perform some work in the background and in this piece of code I am confused as to why the message reading the file... is not displayed before the ReadTheFile(filename) method is called.

private void btnSelect_Click(object sender, EventArgs e)
{
    OpenFileDialog ofd = new OpenFileDialog();
    ofd.CheckFileExists = true;
    ofd.CheckPathExists = true;

    if (ofd.ShowDialog() == DialogResult.OK)
    {
        lblResults.Text = " ... reading the file ...";
        FileReader1 fr = new FileReader1();
        int numLines = fr.ReadTheFile(ofd.FileName);

        lblResults.Text = string.Format("We read {0} lines", numLines.ToString());
    }
}

The author explains it by saying the following, but it did not really get through to me.

Worse, even though we set the label’s Text property before we call ReadTheFile, the message loop doesn’t get a chance to process that change, and update the text, before we go out to lunch in ReadTheFile.

What does this mean? Can this be explained in simpler terms?

Animesh
  • 4,926
  • 14
  • 68
  • 110
  • Has to do with the thread the form is running on. The UI isn't given a chance to repaint before the file is read. – Mike Cheel Nov 22 '12 at 06:28

2 Answers2

2

Worse, even though we set the label’s Text property before we call ReadTheFile, the message loop doesn’t get a chance to process that change, and update the text, before we go out to lunch in ReadTheFile.

Basically you are setting the text of label. However, you then start doing a "intensive" task that could take seconds, minutes, hours. As long as you are continuing to load the file and read the number of lines, the window will not update. That's the point of doing it in a background thread. Let the main thread continue to paint the window and handle UI stuff while your background thread processes the file.

I would continue with the tutorial. Once you get to the part where you start creating and running the background worker you may end up with one of those "Aha!" moments. =)

You may also be interested in reading up on threads in general.

http://www.codeproject.com/Articles/26148/Beginners-Guide-to-Threading-in-NET-Part-1-of-n

http://www.techrepublic.com/article/a-beginners-guide-to-threading-in-c/1044970

TyCobb
  • 8,909
  • 1
  • 33
  • 53
1

You could read Application.DoEvents Method .

When you run a Windows Form, it creates the new form, which then waits for events to handle. Each time the form handles an event, it processes all the code associated with that event. All other events wait in the queue. While your code handles the event, your application does not respond. For example, the window does not repaint if another window is dragged on top.

So, until your btnSelect_Click is finished, your form will not repaint.


I'd edit my answer to remark, that one'd better not use DoEvents explicitly, as it may result in rather weird programm behaviour. (based on comment by J.Skeet).

You could read Use of Application.DoEvents() at SO also for more info. There is an extract from MSDN posted in that thread:

Calling this method causes the current thread to be suspended while all waiting window messages are processed. If a message causes an event to be triggered, then other areas of your application code may execute. This can cause your application to exhibit unexpected behaviors that are difficult to debug. If you perform operations or computations that take a long time, it is often preferable to perform those operations on a new thread.

Community
  • 1
  • 1
horgh
  • 17,918
  • 22
  • 68
  • 123