Link to home
Start Free TrialLog in
Avatar of TSFLLC
TSFLLC

asked on

Display of form hangs during SQL updates - use backgroundworker?

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()
        ShowUpdatesDialog("global_loads")
        RefreshHopperLoads(0)  '      0 = On Login
        ShowUpdatesDialog("global_loads_arc")
        RefreshHopperLoadsArc(0)  '      0 = On Login
        ShowUpdatesDialog("truck")
        RefreshTrucks(0)  '      0 = On Login
        ShowUpdatesDialog("driver")
        RefreshDrivers(0)  '      0 = On Login
        ShowUpdatesDialog("trailer")
        RefreshTrailers(0)  '      0 = On Login
        ShowUpdatesDialog("customer")
        RefreshCustomers(0)  '      0 = On Login
        ShowUpdatesDialog("shipping_address")
        RefreshShippingAddresses(0)  '      0 = On Login
        If glDisplayLoads = 1 Then
             ShowUpdatesDialog("hopper_invoices")
             RefreshHopperInvoices(0)  '      0 = On Login
        Else
             ShowUpdatesDialog("tanker_invoices")
             RefreshTankerInvoices(0)  '      0 = On Login
        End If
        ShowUpdatesDialog("miscellaneous_invoices")
        RefreshMiscInvoices(0)  '      0 = On Login
        ShowUpdatesDialog("close")
    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)
                frm.Close()
                Exit Sub
        End Select
        If myFormsList.ContainsKey(99999) Then
            Dim frm As UpdatesDialog = UpdatesDialog.Instance(Nothing)
            UpdatesDialog.ReUpdateFormDialogTextBox(xText)
            frm.Activate()
            frm.WindowState = FormWindowState.Normal
        Else
            Dim frm As Form = UpdatesDialog.Instance(xText)
            myFormsList.Add(99999, frm)
            frm.Show()
        End If
    End Sub

Open in new window

Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

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.
Avatar of TSFLLC
TSFLLC

ASKER

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.
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.
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of TSFLLC

ASKER

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.

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

ASKER

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?

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.
Avatar of TSFLLC

ASKER

The Application.DoEvents worked flawlessly.

Thanks much!!
Avatar of TSFLLC

ASKER

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