VB.NET Order of Events

Posted on 2011-10-06
Last Modified: 2012-05-12
I have some code in a button click event that run some SQL and returns data. When the button is clicked, I want the label text to say "Please Wait ... running query." The problem is that my label text does not change, it just jumps right into running the query - it is as if it skips changing the label. If I remove the code that comes after the label text change, then it works properly. How do I get it to change the label text first - then run the additional code?
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Label6.Text = "Please Wait ... running query"

            [My Additional Code is here]
        Catch Ex As Exception
            Label6.Text = Ex.ToString
        End Try

End Sub

Open in new window

Question by:NursingCorp
    1 Comment
    LVL 21

    Accepted Solution

    You're running into a threading problem. When your code runs the UI thread it blocked, so even though you're setting the label the UI thread isn't able to update the UI to actually display the change.

    The simplest solution is to allow the message pump to process messages on the queue by calling Application.DoEvents() immediately after you set the label.

    The correct way to solve the problem would be to run the [My Additional Code Here] in a separate thread, but that's a more complex solution.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    Suggested Solutions

    Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now