Results 1 to 31 of 31

Thread: get progress boar progress to the form, from another class.

  1. #1

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,605

    get progress boar progress to the form, from another class.

    Hi.
    So I have a form and the form calls another class.
    The issue is that the progress board is not getting updated with the values (it actually shows but the green counter is not updating).
    Another note. I can see the counter updating if i run it on debug an see one by one the values, but at real time it just show empty.

    Here is some code:
    Code:
    'Form
      Dim LDMS As New List(Of DMS)
            If cGeneral.FillDMS(DtSet, LDMS, Me, iexcelcount) = False Then
             
                Me.Close()
                Exit Sub
            End If
    So I call the FillDMS function from another class.
    Code:
      Friend Function FillDMS(ByVal ds As DataSet, ByRef DMSList As List(Of DMS), ByRef frm As Form2, i As Integer) As Boolean
            Dim Dt As New DateTime
            Dim strDateFrom As String
            Dim dc As Double
    
            frm.ProgressBarExcelLoad.Visible = True
            frm.ProgressBarExcelLoad.Minimum = 1
            frm.ProgressBarExcelLoad.Maximum = i
            frm.ProgressBarExcelLoad.Value = 1
            frm.ProgressBarExcelLoad.Step = 1
    
       Try
    
                For Each row As DataRow In ds.Tables("Voucher").Rows
    
    ''etc
      DMSList.Add(cl)
                    frm.ProgressBarExcelLoad.PerformStep() ' step + 
    next
    
    return
    I used both byref and byval for the form btw.
    any idea?
    thanks.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: get progress boar progress to the form, from another class.

    Firstly, that class should not know anything about any form. The proper way to do that would be for the class to raise an event when the progress of the operation has changed and then to have the form handle that event and update it's own ProgressBar.

    Secondly, you're not going to see any control change when you are executing a loop on the UI thread. I'm surprised that you don't know this already but updating of the UI is done on the UI thread so it can't happen if the UI thread is busy doing something else. You can force it by calling the Refresh method of the control but it would be far better to avoid doing the heavy work on the UI thread in the first place.

  3. #3

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,605

    Re: get progress boar progress to the form, from another class.

    Hmmm.
    OK if I put a thread sleep I can see it loading.
    It probably loads to fast to see or draw.
    Also I have putted everything in the main form and I just left the frm.ProgressBarExcelLoad.PerformStep() is the function.
    I could use a background worked but that is another story.

    I would leave this open for a while so if that is not what needs to be done , comment are welcome.
    Thanks.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: get progress boar progress to the form, from another class.

    You should also know that passing ByRef is of no value whatsoever there. The ONLY reason to declare a parameter ByRef if the data type of that parameter is a class is to be able to assign a new object to the parameter inside the method and have that change affect the original variable. If you're not assigning anything to a reference type parameter in a method then declaring that parameter ByRef CANNOT have ANY effect and should not be done.

  5. #5

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,605

    Re: get progress boar progress to the form, from another class.

    OK.
    Well thanks JMC.
    Won't raising the event slow things down a lot (going back and forth on the form)?

    I should have known about the UI thread but it has been about 7 years since I used winforms so, did not remember.
    thanks for bringing it to my attention.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: get progress boar progress to the form, from another class.

    Quote Originally Posted by sapator View Post
    Hmmm.
    OK if I put a thread sleep I can see it loading.
    It probably loads to fast to see or draw.
    Also I have putted everything in the main form and I just left the frm.ProgressBarExcelLoad.PerformStep() is the function.
    I could use a background worked but that is another story.

    I would leave this open for a while so if that is not what needs to be done , comment are welcome.
    Thanks.
    Actually, there may be some multi-threading built into the ProgessBar itself because I do know that a ProgressBar may not actually display an accurate representation of its Value. That's only going to make things worse though, not better.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: get progress boar progress to the form, from another class.

    Quote Originally Posted by sapator View Post
    Won't raising the event slow things down a lot (going back and forth on the form)?
    I'd suggest that if it did affect things noticeably then the amount of time required to perform the entire task would be so small as to render the use of a ProgressBar pointless.

  8. #8

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,605

    Re: get progress boar progress to the form, from another class.

    OK.
    So ( will see for tomorrow).
    what I do is, raise an event on the function every time a new value is passed and then have the form catch it an update the value?
    That's the idea?
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  9. #9

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,605

    Re: get progress boar progress to the form, from another class.

    Quote Originally Posted by jmcilhinney View Post
    I'd suggest that if it did affect things noticeably then the amount of time required to perform the entire task would be so small as to render the use of a ProgressBar pointless.
    It depends as I will be getting from 5 to 10 loops to 1000 - 10000, so i do not have a static amount of loops.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: get progress boar progress to the form, from another class.

    Quote Originally Posted by sapator View Post
    It depends as I will be getting from 5 to 10 loops to 1000 - 10000, so i do not have a static amount of loops.
    But do you really need to update the ProgressBar on every iteration? You could do but I'm not sure that it would be any more meaningful than just showing a percentage regardless of the total.

  11. #11
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,041

    Re: get progress boar progress to the form, from another class.

    Yeah, I think it's time to fake it a bit. If you are doing something where progress will be made faster than 1/10th of a second, you can't show it anyways. I don't know how gas (or petrol, if you will) dispensers work over there, but we sometimes have digital displays with three digits to the left of the decimal point. I don't know who thought that was a good idea, but the dispensers are fast enough that the hundreds place is little more than a blur, and the thousands is totally unreadable. It's moving, though, so the precision of the dispenser is sufficient, and the interface is fast enough, that the display CAN update for every thousandth of a gallon, even though the people dispensing the gas can't see the numbers as fast as they are changing.

    That's an example of an interface that CAN keep up with the progress being measured, but which should NOT be doing so, because it benefits nobody. The point of a progress bar is to show progress is being made. It should update fast enough that the user doesn't start wondering whether or not the system is frozen, but whether it fills in a second, or takes several seconds, is mostly an aesthetic consideration. Updating every tenth record may be sufficient to show that progress is being made. If so, then updating progress every record will actually waste time.
    My usual boring signature: Nothing

  12. #12
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: get progress boar progress to the form, from another class.

    They did it in the gas pumps for psychological reasons. Jokes about "perfect pump" aside, having that blur means the user expects imprecise results when they stop pumping. Joe Average would be irate if it updated more slowly and never seemed to stop on the value he was seeing, and Joe Average tends to be the one that makes the rules.

    That said, when you're updating a form, you need to be really careful. Think about your form like a video game. People like to shoot for 60 frames per second in video games. That means the game needs to be able to do all of its work in about 17ms. When the frame rate slows down, it's because the game starts taking longer than 17ms and it's drawing frames at a slower interval.

    Your form is like that. If you change the text in a text box, the UI thread has to process a Windows message to do that. And it triggers a WM_PAINT back to the form and the screen has to be drawn. That's also the same thread that has to process mouse clicks and keyboard inputs. Pretend we're on a really slow computer, and it takes 100ms to draw the screen after updating a progress bar. If we're only doing it once per second, we've still got 900ms to do all the other things. But if we do it 9 times per second, we only have 100ms left over to do other things. And if we increase the rate to 10 times per second, there's no time left. At 20 times per second, we end up generating 2 events per frame and never catch up until the process finishes.

    So when you need to send updates to a form, it's important to think about how fast those updates will be arriving. My rule of thumb is I really don't want to update the UI more often than once every 250 milliseconds. Most people can barely perceive updates faster than that, so it's a waste of time and risks making the form seem slow. So if I'm working with, say, a device that updates its readings every 10ms, I write some code to throttle the UI updates down to a reasonable delay. It might look something like this:
    Code:
    Private _throttleTimer As System.Diagnostics.Stopwatch
    
    Private Sub WhenNewReadingsArrive(...)
        _lastReading = e.Data
    
        If _throttleTimer.ElapsedMilliseconds > 250
            SendUpdate()
            _throttleTimer.Reset()
        End If
    End Sub
    I keep the readings as they come in, so if something asks it will get the most recent value, but the UI doesn't really need to be notified as often as the data's coming in so I update in 250ms intervals. There's a lot of code missing in that snippet, and it's not how I always implement a throttle, but it ought to give you a good idea of the technique.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  13. #13

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,605

    Re: get progress boar progress to the form, from another class.

    OK.
    So I do not really care for complete accuracy.
    I can set the progress bar to update every, lets say, 10 iterations.
    Not sure if I must raise an event as JMC suggested as it seems to work without that.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: get progress boar progress to the form, from another class.

    I would tend to just use a percentage. You can declare a property that you set after each iteration and then it can raise an event when the value actually changes, e.g.
    vb.net Code:
    1. Public Class Class1
    2.  
    3.     Private _percentComplete As Integer
    4.  
    5.     Public Property PercentComplete As Integer
    6.         Get
    7.             Return _percentComplete
    8.         End Get
    9.         Private Set(value As Integer)
    10.             If _percentComplete <> value Then
    11.                 _percentComplete = value
    12.                 OnPercentCompleteChanged(EventArgs.Empty)
    13.             End If
    14.         End Set
    15.     End Property
    16.  
    17.     Public Event PercentCompleteChanged As EventHandler
    18.  
    19.     Protected Overridable Sub OnPercentCompleteChanged(e As EventArgs)
    20.         RaiseEvent PercentCompleteChanged(Me, e)
    21.     End Sub
    22.  
    23.     Public Sub ProcessData(data As ICollection(Of Object))
    24.         _percentComplete = 0
    25.  
    26.         Dim itemCount = data.Count
    27.         Dim itemsProcessedCount = 0
    28.  
    29.         For Each item As Object In data
    30.             'Process item here.
    31.  
    32.             itemsProcessedCount += 1
    33.             PercentComplete = CInt(100 * itemsProcessedCount / itemCount)
    34.         Next
    35.     End Sub
    36.  
    37. End Class
    You can then handle that PercentCompleteChanged event in your form and get the PercentComplete property value to update the ProgressBar. With the Minimum and Maximum of the ProgressBar set to 0 and 100 respectively, that will just work, regardless of the amount of data.

  15. #15

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,605

    Re: get progress boar progress to the form, from another class.

    Lot's of work for a small progress change but OK, will try it out and post back if any issues.
    Will have to find some data with more than just 10-20 rows so I can see the change.
    Thanks.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  16. #16

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,605

    Re: get progress boar progress to the form, from another class.

    OK.
    Spoke to soon.
    I don't get any event hit on form2.
    Probably doing something wrong.
    Code:
    Public Class Class1
    
    #Region "percentage4progressBar"
        Private _percentComplete As Integer
    
        Public Property PercentComplete As Integer
            Get
                Return _percentComplete
            End Get
            Private Set(value As Integer)
                If _percentComplete <> value Then
                    _percentComplete = value
                    OnPercentCompleteChanged(EventArgs.Empty)
                End If
            End Set
        End Property
    
        Public Event PercentCompleteChanged As EventHandler
    
        Protected Overridable Sub OnPercentCompleteChanged(e As EventArgs)
            RaiseEvent PercentCompleteChanged(Me, e)
        End Sub
    #End Region
    
    .....etc
     Friend Function blah
       Dim itemCount = data.Count
            Dim itemsProcessedCount = 0
      For Each something in data
    calculations()
      itemsProcessedCount += 1
                    PercentComplete = CInt(100 * itemsProcessedCount / itemCount)
                    RaiseEvent PercentCompleteChanged(Me, Nothing) ' or (me,PercentComplete ) ? 
                'or we need?    Application.DoEvents()
    next
    end function
    Form2
    Code:
     Dim WithEvents cGg As new Class1
    
    Private Sub PercentageComplete(sender As Object, e As System.EventArgs) Handles cGg.PercentCompleteChanged
    'WILL NOT HIT
        End Sub
    Thanks.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  17. #17
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: get progress boar progress to the form, from another class.

    Firstly, get rid of that RaiseEvent statement in that loop. I don't have that for a reason. As I said in an earlier post, the point is that you just set the property and then it will raise the event if an when necessary.

    As for your issue, have you debugged the code? Is the event actually being raised? If the event is raised but your event handler is not executed then the first thing that comes to my mind is that you're handling the event of the wrong instance.

  18. #18

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,605

    Re: get progress boar progress to the form, from another class.

    OK.
    You are right I was handling it in a class with the same name so it did not have the withevents.
    Now the issue is how would I go about on the Private Sub PercentageComplete(sender As Object, e As System.EventArgs) Handles cGeneral.PercentCompleteChanged

    I set the ProgressBar.Value = sender.PercentComplete
    but I also need to do a performstep. when should i do that. Obviously not in every event handler. I think i need to do that in every 10 of a percentage but I'm not good with maths.
    any help?
    thanks.

    I use on form2
    ProgressBar.min = 1
    ProgressBar.max =100
    ProgressBar.Step = 10
    and i handle the .value in the event but when should I use the .performstep?
    Thanks.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  19. #19

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,605

    Re: get progress boar progress to the form, from another class.

    OK. The event just hit once here.
    Hmmm....
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  20. #20
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: get progress boar progress to the form, from another class.

    Quote Originally Posted by sapator View Post
    I also need to do a performstep.
    Why would you think that? The PercentComplete property contains the percentage of the process that is complete and you assigning that to the Value of the ProgressBar. Why would you need to do anything else?

  21. #21

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,605

    Re: get progress boar progress to the form, from another class.

    Yes.
    I just saw that. I do not need it.
    So everything is fine but I have a question here.
    How can I divide to 10 steps instead of 100?
    Seems that the process is fast so even with a value of 5000 it shows "ugly"
    So I would appreciate 10 steps.
    Thanks.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  22. #22

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,605

    Re: get progress boar progress to the form, from another class.

    Oh.
    Should just use PercentComplete = CInt(10 * itemsProcessedCount / itemCount) ?
    Is that correct?
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  23. #23
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: get progress boar progress to the form, from another class.

    Quote Originally Posted by sapator View Post
    Oh.
    Should just use PercentComplete = CInt(10 * itemsProcessedCount / itemCount) ?
    Is that correct?
    That's correct, but you should probably change the name of the property and event because it's no longer percentage.

  24. #24

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,605

    Re: get progress boar progress to the form, from another class.

    Thanks.
    So finally the only issue i get is that the bar will not fill completely when it gets to 10.
    I mean, when ProgressBar.Value = sender.PercentComplete goes to 10, the bar will not fill completely, will just sty one step back.
    Is that an expected behavior?
    thanks.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  25. #25

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,605

    Re: get progress boar progress to the form, from another class.

    Hey.
    I don't wont to bother you no more so I just use this on the event hit:
    Code:
      If sender.PercentComplete > 10 Then
                Exit Sub
            End If
    
       If sender.PercentComplete >= 9 Then
                ProgressBar.PerformStep()
                Exit Sub
            End If
    This works.So if you are busy don't let me take your time for this as it is a minor glitch.
    Thanks for all the support
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  26. #26

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,605

    Re: get progress boar progress to the form, from another class.

    Hmmm.
    and again Hmmm.
    OK so even with this , when i put a thread.sleep on form 2 AFTER the calculations and the progessBar completes it will only show one step complete.
    So is this a speed thing or something else?
    thanks.
    Last edited by sapator; Apr 25th, 2017 at 05:40 AM.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  27. #27

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,605

    Re: get progress boar progress to the form, from another class.

    Hmmm.
    and again Hmmm.
    OK so even with this , when i put a thread.sleep on form 2 AFTER the calculations and the progessBar completes it will only show on step complete.
    So is this a speed thing or something else?
    thanks.
    I'm inclined to just remove the damn thing and get it over with.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  28. #28
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: get progress boar progress to the form, from another class.

    Let's make a full-fledged example to make sure you're not crossing any wires, and to make sure each concept and moving part gets explained.

    Code:
    Imports System.ComponentModel
    
    Public Class Form1
    
        Private _rng As New Random()
        Private _worker As ThreadWorker
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            ' Set up controls for a new run.
            Button1.Enabled = False
            ProgressBar1.Value = 0
    
            ' Create the new worker and handle the change and completed events.
            _worker = New ThreadWorker()
            AddHandler _worker.ProgressChanged, AddressOf WhenProgressChanges
            AddHandler _worker.RunWorkerCompleted, AddressOf WhenWorkerFinishes
    
            ' Give the worker something to do.
            _worker.RunWorkerAsync(_rng.Next(1000, 2000))
        End Sub
    
        ' This event is raised when the worker calls ReportProgress(), and happens on the UI thread.
        Private Sub WhenProgressChanges(sender As Object, e As ProgressChangedEventArgs)
            Dim progress As Integer = e.ProgressPercentage
            ProgressBar1.Value = progress
        End Sub
    
        ' This event happens after all work completes, and happens on the UI thread.
        Private Sub WhenWorkerFinishes(sender As Object, e As RunWorkerCompletedEventArgs)
            ' The last progress event is rarely 100, so manually set it. Also re-enable the button.
            ProgressBar1.Value = 100
            Button1.Enabled = True
    
            _worker = Nothing
        End Sub
    End Class
    
    Public Class ThreadWorker
        Inherits BackgroundWorker
    
        Public Sub New()
            WorkerReportsProgress = True
        End Sub
    
        ' This is called on the worker thread when the user calls RunWorkerAsync().
        ' When it finishes, the RunWorkerCompleted event is raised.
        Protected Overrides Sub OnDoWork(e As DoWorkEventArgs)
            MyBase.OnDoWork(e)
    
            Dim throttleWatch = Stopwatch.StartNew()
            Dim countTo As Integer = CInt(e.Argument)
            For i As Integer = 0 To countTo
                If throttleWatch.ElapsedMilliseconds > 250 Then
                    ReportProgress(CInt(Math.Floor(i / countTo * 100)))
                    throttleWatch.Start()
                End If
    
                ' I did this JUST for the example, so the counting doesn't finish
                ' immediately. Computers are fast.
                Threading.Thread.Sleep(5)
            Next
        End Sub
    
    End Class
    I haven't really seen a lot of your code, but this is the basic pattern for a progress bar that displays something. It can be implemented a few other ways, but you have to understand how the bits work to branch out. If your code isn't working, odds are it's because it doesn't follow this basic pattern. I can't offer much specific advice about fixing that, based on the code I can see in the thread.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  29. #29

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,605

    Re: get progress boar progress to the form, from another class.

    Hi.
    I currently using application.doevents as this is a 1% of the total work i need to do.
    I know this can break the application but it is a fast - dirty solution just for now and if I have time I will try to integrate the examples.
    I removed the ProgressBar from the function (JMC example) as it is not updating, maybe it needs threading but kept the percentage solution of JMC as I really liked it.
    As I'm re-learning forms development I remember using the ProgressBar on a background worker. Strangely I've read that the UI is not updated on the BGW but maybe (as JMC said), the ProgressBar may have some multi threading so it is updated. Do not know for sure.
    Thanks
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  30. #30
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: get progress boar progress to the form, from another class.

    Quote Originally Posted by sapator View Post
    As I'm re-learning forms development I remember using the ProgressBar on a background worker. Strangely I've read that the UI is not updated on the BGW
    The point of the BackgroundWorker is to do the work on a background thread so that the UI remains responsive to do UI things, e.g. update a ProgressBar. You call RunWorkerAsync and handle the DoWork event, which is raised on a background thread. You do your work in that event handler and, to update the UI as you go, you call ReportProgress and handle the ProgressChanged event, which is raised on the UI thread. That means that you can update the UI in that event handler. If you want to perform a final update on the UI when the operation is complete, you handle the RunWorkerCompleted event, which is also raised on the UI thread. As an example, you might display a ProgressBar and then call RunWorkerAsync, update progress as you go and then hide the ProgressBar again in the RunWorkerCompleted event handler. You can find an example of using a BackgroundWorker if you follow the link below to my CodeBank threads.

  31. #31

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,605

    Re: get progress boar progress to the form, from another class.

    Hey.
    As I've said I'm sure I have done this in the past (waaaay in the past).
    Now I'm temped to change to whole progress bar code but I'm in a very tight schedule.....
    Hmmm.

    But, thanks anyhow
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

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