execution order

matm

New member
Joined
Jul 22, 2009
Messages
4
Programming Experience
Beginner
Hi there,

This is probably a pretty basic question, but I've just noticed that the execution order for my VB.NET app doesn't seem to be correct.

I've created a clean app with just proof of concept code to confirm that it occurs in a fresh project, and it does.

The code is...

VB.NET:
Imports System.IO

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        TextBox1.Text = "Form opened"

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        TextBox1.Text = "Pre-operation text"

        Dim i As Integer

        i = 0

        While i < 500

            Directory.GetFiles("C:\Windows\System32")
            i = i + 1

        End While

        TextBox1.Text = "Post operation text"

    End Sub

End Class

When the form loads, the text box reads "Form opened" as expected. However, when you click Button1, there is a delay while the processing occurs, and then the text box reads "Post operation text".

It completely skips the "Pre-operation text", which I don't understand? Can someone please explain what I might be doing wrong?
 
Ran your code with one small modification and it ran fine for me. I added code to refresh the textbox after the text was changed.

Sounds like a graphical issue to me.
 
Hi there,

This is probably a pretty basic question, but I've just noticed that the execution order for my VB.NET app doesn't seem to be correct.

I've created a clean app with just proof of concept code to confirm that it occurs in a fresh project, and it does.

The code is...

VB.NET:
Imports System.IO

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        TextBox1.Text = "Form opened"

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        TextBox1.Text = "Pre-operation text"

        Dim i As Integer

        i = 0

        While i < 500

            Directory.GetFiles("C:\Windows\System32")
            i = i + 1

        End While

        TextBox1.Text = "Post operation text"

    End Sub

End Class

When the form loads, the text box reads "Form opened" as expected. However, when you click Button1, there is a delay while the processing occurs, and then the text box reads "Post operation text".

It completely skips the "Pre-operation text", which I don't understand? Can someone please explain what I might be doing wrong?
You never give the form time to update before doing that cpu intensive loop that ties up the UI thread completely until it's done.

You could put in a Me.Refresh or Application.DoEvents, but with that loop all you're essentially doing is a hack to fake the UI being responsive to the user.

What I would do is use a BackgroundWorker to do that loop on a background thread.
 
Thanks guys, that makes sense now. The CPU intensive operation was just an example, the actual app didn't do that, but the UI refresh makes perfect sense.

Thank you for clearing that up.
 
As a side note, whenever you have any heavy cpu and/or IO operations you should always consider putting it on a background thread, if anything, to allow the form(s) to remain 100% responsive to the user all the time.

Another reason is that most people have dual or quad core cpu's, your app can potentially utilize that to make the operations much quicker.
 
Yep, thanks. I had actually done that in the code that I didn't post (created an asynchronous background worker) but it still seemed to have the problem.

I think I was doing something wrong, though, so I might be posting that code for some help soon.

Thanks again, really appreciate it.
 
Yep, thanks. I had actually done that in the code that I didn't post (created an asynchronous background worker) but it still seemed to have the problem.

The paint events are probably being coalesced
 
Back
Top