Display of form hangs during SQL updates - use backgroundworker?

Posted on 2009-04-28
Last Modified: 2012-05-06
I want to display a 'routine status' form that displays as my code below reflects in a form during the execution of certain Subs/Functions.  The only problem is that it displays but hangs and does not show the text box(s) until the routine is basically finished.  I have several updates that take about 5-6 seconds because of their complexity.  I need to be able to inform the user that the routine is not finished and DON'T CLICK the 'X' to CLOSE FORM by continuing to display text messages infoming them of the status.

I've seen several examples of using a backgroundworker but I can't wrap my head around it where it can be used with a Sub Routine.  And I'm not sure if that's the best way to tackle it anyway.

Please help.

Public Sub SetUserGlobals()


        RefreshHopperLoads(0)  '      0 = On Login


        RefreshHopperLoadsArc(0)  '      0 = On Login


        RefreshTrucks(0)  '      0 = On Login


        RefreshDrivers(0)  '      0 = On Login


        RefreshTrailers(0)  '      0 = On Login


        RefreshCustomers(0)  '      0 = On Login


        RefreshShippingAddresses(0)  '      0 = On Login

        If glDisplayLoads = 1 Then


             RefreshHopperInvoices(0)  '      0 = On Login



             RefreshTankerInvoices(0)  '      0 = On Login

        End If


        RefreshMiscInvoices(0)  '      0 = On Login


    End Sub

    Public Sub ShowUpdatesDialog(ByVal strDetails As String)

        Dim xText As String = ""

        Select Case strDetails

            Case "global_loads"

                xText = "Accessing/updating truck load records."

            Case "global_loads_arc"

                xText = "Accessing archived truck load records."

            Case "truck_load"

                xText = "Updating new/existing truck load record."

            Case "truck"

                xText = "Updating new/existing truck record."

            Case "truck_status"

                xText = "Updating truck status record(s)."

            Case "driver"

                xText = "Updating new/existing driver record."

            Case "driver_status"

                xText = "Updating driver status record(s)."

            Case "trailer"

                xText = "Updating new/existing trailer record."

            Case "trailer_status"

                xText = "Updating trailer status record(s)."

            Case "contact"

                xText = "Updating new/existing contact record."

            Case "customer"

                xText = "Updating new/existing customer/billing address record."

            Case "shipping_address"

                xText = "Updating new/existing pickup/delivery shipping address record."

            Case "hopper_invoices"

                xText = "Accessing/updating hopper load invoice record(s)."

            Case "tanker_invoices"

                xText = "Accessing/updating tanker load invoice record(s)."

            Case "misc_invoicing"

                xText = "Accessing/updating miscellaneous invoice record(s)."

            Case "repopulating_fields"

                xText = "Repopulating truck load record."

            Case "close"

                xText = ""

                Dim frm As UpdatesDialog = UpdatesDialog.Instance(Nothing)


                Exit Sub

        End Select

        If myFormsList.ContainsKey(99999) Then

            Dim frm As UpdatesDialog = UpdatesDialog.Instance(Nothing)



            frm.WindowState = FormWindowState.Normal


            Dim frm As Form = UpdatesDialog.Instance(xText)

            myFormsList.Add(99999, frm)


        End If

    End Sub

Open in new window

Question by:TSFLLC
    LVL 83

    Expert Comment

    To be able to use a backgroundworker control, you would need to add the control to the form and then implement 3 of its events. You would write all the code above in the BackGroundWorker1_DoWork event. Then within this code, you will use the ReportProgress method of the control at different places to update the progress. The progress will be updated by implementing the UpDateProgress event. Finally, use the WorkCompleted event to notify the user and hide the progress form.

    Author Comment

    My apologies.  The one thing I neglected to add was that this is a procedure in my Modules.vb.
    There is no one form specifically associated with it.

    This is where it causes me the grief.
    LVL 83

    Expert Comment

    In that case you need to use Threads. The annoying thing with threads for me is to having to use the PInvoke for updating progress.
    LVL 83

    Accepted Solution

    Another option is to use the Application.DoEvents statement at intervals within the code. This statement forces the app to refresh itself and process and window messages so it would update the labels etc if you are using them. You can also stop user from closing the app by displaying a modal form. So you could use a small form showing "Update in progress..." for example and show it as modal. The user would not be able to close the question unless you hide this form after completing the execution of sub.

    Author Comment

    The second option seems to be the best....if it doesn't use much overhead nor slows down the original process (SQL updates, selects, etc.)

    The modal window is already created and a single label would be the only thing refreshed.  Ah, as I am writing this I realize that I don't need to do anything but refresh the lbl with form.instance.lblProgress.text = "?" as I continue showing subsequent messages.

    I'll have to read up on Application.DoEvents.  I've seen it but don't know squat about how to code it.

    LVL 83

    Expert Comment

    Just put this statement between the execution of other code. I mean you could may be replace the ShowUpdateDialog with this statement.

    Author Comment

    Can't do that.  The ShowUpdateDialog does the following:

    1) Sets the text value, passes it to my form
    2) Determines whether the form is open, if is pass value, else .Show
    3) Determines whether it's time to close the form

    I think I could put it in the code at the bottom of the ShowUpdateDialog once the form is opened OR text field has been updated.   Look up at the ShowUpdateDialog sub again.

    Sound appropriate?

    LVL 83

    Expert Comment

    Yeah you could put it inside that sub. Try experimenting with it by placing it at different places and see what difference does it make.

    Author Comment

    The Application.DoEvents worked flawlessly.

    Thanks much!!

    Author Closing Comment

    Thanks,  you and all the others at EE that have helped have saved me from COUNTLESS hours of a bruised skull.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
    A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    779 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

    14 Experts available now in Live!

    Get 1:1 Help Now