Threading in Visual Studio 2008 - They disappear and don't come back!

visual studio , .net , backgroundworker , threading , odbc , myob Sydney, Australia
  • 11 years ago

    I am using the following code which works OK in a single thread as shown below, however, when I try to load the datatables asynchonously (by commenting out the synchonous and commenting in the asynchronous in FillDataSet) the first thread never comes back. I know by steping that it gets as far as filling the datatable in GetJobs but after that it dissapears. Does anyone know why?

    BTW the data link is SLOW - it takes close to a minute to fill the tables (137 & ~100 rows) when the form loads synchonously. I was hoping that an asynchronous approach would speed up the app initilisation but so far it doesn't work and I'd rather have a slow app that works than a fast one that doesn't

        Imports System.ComponentModel
    
        Public Class FormJobNumber
            Private pJobsLoaded As Boolean = True
            Private pPurchasesLoaded As Boolean = True
    
            Public ReadOnly Property JobsLoaded() As Boolean
                Get
                    Return pJobsLoaded
                End Get
            End Property
    
            Public ReadOnly Property PurchasesLoaded() As Boolean
                Get
                    Return pPurchasesLoaded
                End Get
            End Property
    
            Public Sub FillDataSet(ByVal SupplierInfo As Supplier)
                GetJobs(Nothing, Nothing)
                GetSuppliers(SupplierInfo, Nothing, Nothing)
                'BackgroundWorkerJobs.RunWorkerAsync()
                'BackgroundWorkerSuppliers.RunWorkerAsync(SupplierInfo)
            End Sub
    
            Private Function GetJobs(ByVal worker As BackgroundWorker, ByVal e As DoWorkEventArgs) As Integer
                Try
                    Return Me.JobsDataAdapter.Fill(Me.Myob1.Jobs)
                Catch ex As Exception
                    MsgBox("Error: " & ex.Message & vbCrLf & "Getting Jobs from MYOB", MsgBoxStyle.Critical, "Error")
                    Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
                    Me.Hide()
                    Return -1
                End Try
            End Function
    
            Private Function GetSuppliers(ByVal SupplierInfo As Supplier, ByVal worker As BackgroundWorker, ByVal e As DoWorkEventArgs) As Integer
                Try
                    Me.PurchasesDataAdapter.SelectCommand.Parameters("SupplierName").Value = SupplierInfo.FullName
                    Me.PurchasesDataAdapter.SelectCommand.Parameters("StartDate").Value = Now.AddMonths(-1).AddDays(-Now.Day).ToString("MM/dd/yyyy")
                    Return Me.PurchasesDataAdapter.Fill(Me.Myob1.Purchases)
                Catch ex As Exception
                    MsgBox("Error: " & ex.Message & vbCrLf & "Getting Jobs from MYOB", MsgBoxStyle.Critical, "Error")
                    Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
                    Me.Hide()
                    Return -1
                End Try
            End Function
    
            Private Sub BackgroundWorkerJobs_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorkerJobs.DoWork
                Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
                pJobsLoaded = False
                e.Result = GetJobs(worker, e)
            End Sub
    
            Private Sub BackgroundWorkerJobs_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorkerJobs.RunWorkerCompleted
                pJobsLoaded = True
            End Sub
    
            Private Sub BackgroundWorkerSuppliers_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorkerSuppliers.DoWork
                Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
                pPurchasesLoaded = False
                While Not Me.JobsLoaded
                End While
                e.Result = GetSuppliers(e.Argument, worker, e)
            End Sub
    
            Private Sub BackgroundWorkerSuppliers_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorkerSuppliers.RunWorkerCompleted
                pPurchasesLoaded = True
            End Sub
        End Class
    

Post a reply

No one has replied yet! Why not be the first?

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“There are only two kinds of languages: the ones people complain about and the ones nobody uses” - Bjarne Stroustrup