Results 1 to 8 of 8

Thread: [RESOLVED] Progress bar not updating when BackgroundWorker ReportProgress from module

  1. #1

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,145

    Resolved [RESOLVED] Progress bar not updating when BackgroundWorker ReportProgress from module

    Consider the code below for a simple form with a progress bar, a button, and BGW is a BackgroundWorker with Modifiers = Public. This works but if I change blnRunInModule to true it doesn’t update. Yet the PerformStep is being executed as is apparent by the Debug.WriteLine showing the value increasing. What am I doing wrong?
    vb.net Code:
    1. Public Class Form1
    2.     Private Sub Button1_Click() Handles Button1.Click
    3.         BGW.RunWorkerAsync()
    4.     End Sub
    5.     Private Sub BGW_DoWork() Handles BGW.DoWork
    6.         Dim blnRunInModule As Boolean = False
    7.         For intCount As Integer = 1 To 10
    8.             If blnRunInModule Then
    9.                 Module1.MySub(intCount)
    10.             Else
    11.                 BGW.ReportProgress(intCount)
    12.             End If
    13.             System.Threading.Thread.Sleep(1000)
    14.         Next
    15.     End Sub
    16.     Private Sub BGW_ProgressChanged() Handles BGW.ProgressChanged
    17.         ProgressBar1.PerformStep()
    18.         Debug.WriteLine("Step executed. Value = " & ProgressBar1.Value)
    19.     End Sub
    20. End Class
    vb.net Code:
    1. Module Module1
    2.     Sub MySub(intCount As Integer)
    3.         Form1.BGW.ReportProgress(intCount)
    4.     End Sub
    5. End Module

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Progress bar not updating when BackgroundWorker ReportProgress from module

    Crossing threads, I suspect, although I'm a little surprised you're not raising an exception. Nothing good ever comes of accessing external functions or subs from within the background worker. Clearly in this case you're blocking the paint events for the progress bar in so doing. Far less innocuous effects could occur. What happens in the Background Worker stays in the Background Worker!
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: Progress bar not updating when BackgroundWorker ReportProgress from module

    As I understand it, it's because you are updating the wrong instance of Form1 when you use the code in the Module.

    I think this thread from last year will explain your problem.

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Progress bar not updating when BackgroundWorker ReportProgress from module

    Quote Originally Posted by Inferrd View Post
    As I understand it, it's because you are updating the wrong instance of Form1 when you use the code in the Module.

    I think this thread from last year will explain your problem.
    Don't think that can be true because, as the OP says, the ProgressBar Value is updated. It's just that the paint of the bar doesn't happen.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  5. #5
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: Progress bar not updating when BackgroundWorker ReportProgress from module

    Quote Originally Posted by dunfiddlin View Post
    Don't think that can be true because, as the OP says, the ProgressBar Value is updated. It's just that the paint of the bar doesn't happen.
    Yes, it's a progress bar on a different instance of Form1, and that instance isn't being shown. Have a read of the thread.

  6. #6
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Progress bar not updating when BackgroundWorker ReportProgress from module

    Ok, MS weirdness. Not sure I still fully understand it but I guess it makes more sense than my suggestion. Either way, I'll stick to my last stated principle if only for a quiet life!
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  7. #7

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,145

    Re: Progress bar not updating when BackgroundWorker ReportProgress from module

    I saw that post of John's and I'm beginning to see the light a little. Also with what dunfiddlin said on my other post. I can see I'm going down the wrong path here so I'll try another tack by creating a class to contain the BackgroundWorker.

  8. #8

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,145

    Re: Progress bar not updating when BackgroundWorker ReportProgress from module

    The problem in the original example is when I execute the Form1.BGW.ReportProgress it’s not getting to the correct Form1. It’s finding the ProgressChanged under the default instance of Form1 which is a copy for the worker thread I guess because it’s being passed ByVal. Since this copy of Form1 is on the worker thread it’s executing it’s ProgressChanged in that form which is on the worker thread. My mistake was adding “Form1.” In front of ReportProgress in the module.

    I remembered that passing a parameter can be done ByRef instead of the default. This in fact creates a new variable which simply contains the address of the original object and therefore doesn’t create another instance and the called procedure can modify the original object where it was called from. IE it doesn’t create a copy of the object but rather points back to the original. That was all I needed. Now in the module when I reference BGW.whatever it’s using the original one which gets the correct ProgressChanged executed. Note I now pass the BackgroundWorker to the module by reference and removed the "Form1."
    vb.net Code:
    1. Imports System.ComponentModel
    2. Module Module1
    3.     Sub MySub(intCount As Integer, ByRef BGW As BackgroundWorker)
    4.         BGW.ReportProgress(intCount)
    5.     End Sub
    6. End Module
    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Button1_Click() Handles Button1.Click
    4.         ProgressBar1.Value = 0
    5.         BGW.RunWorkerAsync()
    6.     End Sub
    7.  
    8.     Private Sub BGW_DoWork() Handles BGW.DoWork
    9.         For intCount As Integer = 1 To 10
    10.             If chkRunInModule.Checked Then
    11.                 Module1.MySub(intCount, BGW)
    12.             Else
    13.                 BGW.ReportProgress(intCount)
    14.             End If
    15.             System.Threading.Thread.Sleep(1000)
    16.         Next
    17.     End Sub
    18.  
    19.     Private Sub BGW_ProgressChanged() Handles BGW.ProgressChanged
    20.         ProgressBar1.PerformStep()
    21.     End Sub
    22.  
    23. End Class

Tags for this Thread

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