Results 1 to 23 of 23

Thread: .NET 5 BGW Never Finishes in Prod Only

Threaded View

  1. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,398

    Re: .NET 5 BGW Never Finishes in Prod Only

    The way the program works is that the user can choose one or more items from a listbox. They then press a button. For each item selected, a BackGroundWorker is launched to get data from a service for that item.
    This is a code smell. Why not start one background worker and loop over each item? If you want them to be processed simultaneously you would leverage a parallelism paradigm. What I suspect is happening is that one or more background workers are failing and so your form variable never reaches 0.

    Reading your post a bit more, this plays into your suspicion as to what is happening.

    With that being said, what are you doing in terms of error handling? Because if one of the workers fail, then I would suspect one of two things to happen:
    1. The entire process is stopped and the user is notified
    2. One "batch" doesn't stop the process, but it still decreases form variable value.


    Update
    So for example, if you were to setup your background worker to do the following:
    Code:
    Private Sub MyBackgroundWorker_DoWork(sender As Object, e As DoWorkEventArgs) Handles MyBackgroundWorker.DoWork
        For counter As Integer = 0 To SelectedItems.Length - 1
            Dim failedRequest As Object = Nothing
            Try
                ' do something with the item
            Catch ex As Exception
                failedRequest = SelectedItems(counter)
            Finally
                MyBackgroundWorker.ReportProgress(100 * counter / SelectedItems.Length, failedRequest)
            End Try
        Next
    End Sub
    
    Private Sub MyBackgroundWorker_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles MyBackgroundWorker.DoWork
        If (e.UserState IsNot Nothing) Then
            ' e.UserState failed, do something (or not)
        End If
    End Sub
    Last edited by dday9; Mar 2nd, 2022 at 05:03 PM.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width