Question ToolSTripStatusLabel doesn't update

tivie

New member
Joined
Feb 18, 2010
Messages
2
Programming Experience
Beginner
Hey there.

First of all, let me say i'm relatively new to .NET.

Anyways, I'm making a program that searches IMDB online for the episode list.

Since the program access the web, it takes ages sometimes to actually complete a task. I wanted to inform the user that the program is still busy and he will have to pantiently wait and so I added a StatusLabel in the StatusStrip.

The problem is that that the Status isn't updated while computer is... humm... "thinking".

Here's the piece of code:


VB.NET:
    Private Sub Search_IMDB_URL_CNP_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Search_IMDB_URL_CNP_Button.Click

        StatusLabel_CNP.Text = "Busy"

       ' If I insert a line like
       ' MsgBox("some text!")
       ' The status is actually upgraded

        If Series_Name_CNP_TB.Text = "" Then

            Series_Name_CNP_TB.Text = InputBox("Series Name Field is empty. Please input a name and click 'Search' again", "Series Name Missing")

            Series_Name_CNP_TB.Text = Series_Name_CNP_TB.Text.Trim

        Else

            Dim IMDBQuery As New IMDB.SearchMode(Series_Name_CNP_TB.Text)

            If IMDBQuery.NumberOf_SearchMatchesFound = 0 Then

                MsgBox("No matches found!")

                IMDB_URL_CNP_TB.Text = ""

                StatusLabel_CNP.Text = "Ready"
                Exit Sub

            End If



            If IMDBQuery.NumberOf_SearchMatchesFound = 1 Then

                IMDB_URL_CNP_TB.Text = IMDBQuery.EpisodeListPage_URL

            Else

                Dim Search_IMDB_form As New SearchIMDB_Dialog(Series_Name_CNP_TB.Text, IMDBQuery.NumberOf_SearchMatchesFound)

                If Search_IMDB_form.ShowDialog(Me) = DialogResult.OK Then IMDB_URL_CNP_TB.Text = Search_IMDB_form.IMDB_URL_SID

            End If



        End If
        StatusLabel_CNP.Text = "Ready"

    End Sub


If I actually add a line like msgbox("something") the status is upgraded, ut if I don't the label isn't and remains saying "Ready".

I can easily workaround this, like warpping the code in a sub and point the buttonclick event to run that sub.

But my main goal is to understand why the status isn't updated if it appears before the rest of the code.
 
Because you're running in a single thread. The thread is busy querying the website and therefore can't update the form.
The simple, hack solution is to add the line Application.DoEvents() where you had the MsgBox (IMO, you should have used the native .Net MessageBox class instead). DoEvents will allow your application to processes all Windows messages currently in the message queue (Windows messages such as the one instructing your form to repaint it's graphics).

The better solution is to use a background thread to query the website.
 
Ah I see. I thougt it would be something like that.

So, the Window paiting is always queued last?
For instance, while the program is "running" code, no changes are made to the window until it finishes?

Quite interesting.

So, the solution would be either creating a new thread for the query, so that in runs in the back, or force the program to "pause" the code execution, like adding a message box, for instance, which would require user input and thus preventing the code to run while the OK button isn't pressed.


VB.NET:
Dim NewThread as New System.Threading.Thread(Address of QueryIMDB)

'then call the procedure somewhere


As a side note, what is the difference between MsgBox("some text") and MessageBox.Show("some text")?

Thanks a lot for your help and sorry for my non-tech langauge ;)
 
So, the Window paiting is always queued last?
For instance, while the program is "running" code, no changes are made to the window until it finishes?
The point is the message queue is not processed at all while your event handler code is executing. So when that takes some time it should be done in secondary thread to free the UI thread quickly. You can use the different System.Threading tools for this or BackgroundWorker component from Toolbox.
 
To answer your question regarding MsgBox("some text") and MessageBox.Show("some text"):

If you look at the Microsoft.VisualBasic.MsgBox() function using a reflector, you will see it does some exception checking then passes the call to System.Windows.Forms.MessageBox.Show() which does the same exception checking. So calling System.Windows.Forms.MessageBox.Show() directly removes the redundant exception checking. Also, using the Microsoft.VisualBasic namespace reduces the ability to port your applications to other .NET languages. There are different schools of thought on this. IMO = In My Opinion.
 
Back
Top