[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Display of form hangs during SQL updates - use backgroundworker?

Posted on 2009-04-28
Medium Priority
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
  • 5
  • 5
LVL 83

Expert Comment

ID: 24261070
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

ID: 24261172
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

ID: 24261474
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.
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 83

Accepted Solution

CodeCruiser earned 2000 total points
ID: 24261530
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

ID: 24261732
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

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

Author Comment

ID: 24262157
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

ID: 24262171
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

ID: 24273766
The Application.DoEvents worked flawlessly.

Thanks much!!

Author Closing Comment

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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

873 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