Page 2 of 2 FirstFirst 12
Results 21 to 22 of 22

Thread: Cannot get Progress Bar to Update while running tasks in WPF

  1. #21
    CodeLiftsleep is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.5
    Join Date
    Mar 2016
    Posts
    20
    Reputation
    15
    Quote Originally Posted by jmcilhinney View Post
    Why aren't you copying and pasting your actual code?
    I did, but I must have changed it in the program after pasting it once it threw an error and then forgot to change it here.

    Anyways, I have somewhat figured out what is going wrong.

    I have a third moving part involved.

    I have a GeneratePlayers Page, the ViewModel and then the ProgressBarDialog Window

    Basically here is what I want to happen. When the user clicks the button to Generate Players from the GeneratePlayers page, that page gets disabled and it Opens up a ProgressBarDialong Window on top of it saying "Generating Players...." and then the progress bar updates while its generating players until its finished, at which point it closes itself and control returns back to the GeneratePlayers Page. I got the opening and closing, etc working but not the updating of the progress bar.

    Here is what I need to happen:

    1) GeneratePlayers page opens up a new instance of the ProgressBarDialog Window once the button is clicked
    2) ProgressBarDialog instance stays open and updates itself from the ViewModel Properties that its bound to.
    3) ViewModel however is getting its values from the GeneratePlayers Page, which I believe is making it so two separate instances of the ViewModel are being used...Ie, the GeneratePlayers instance is updating the values, but the ProgressBarDialong instance is reading its own values which are not being updated and are always 0. Do I need to make the ViewModel NotInheritable and its Properties/Methods as Shared? Make it a Module? VB is such a pain in the ass compared to C# where I could just make it static....

    Whats the easiest way to make these 3 parts work together the way they need to?

    UPDATE: OK, I added some code to help me see what is going on...

    I have it printing to the console the System.Threading.Thread.CurrentThread.ManagedThrea dID
    So the original GeneratePlayers Thread is 8 for example, then I call:

    --->Prog.Show() --->On Thread 8
    --->Window.GetWindow(Prog) ----> On Thread 8

    Code:
    Await Task.Run(Sub()
    
    
                               Console.WriteLine($"This is the current thread 2: { System.Threading.Thread.CurrentThread.ManagedThreadId}")
                               TimeIt(Sub() GenNewPlayersASync(MyVM, Prog))
                           End Sub)
    Which along with declaring the ButtonClick as Async frees up the Progress Bar Window so I can move it around, etc when it pops up(its not stuck like it was before).
    This and everything past this becomes Thread 10...

    and checking Prog.Dispatcher.CheckAccess() inside Thread 10 is returning False(which makes sense since its on Thread 8)...which means I need to run Prog.Dispatcher.BeginInvoke to gain access to it and then update the UI from within it, correct?


    I've also seen where they say to open the window inside the thread you need it to be in but then it throws an error saying the method needs to declared as an <STAThread because many UI components require this...so I'm not sure which way to go about this...
    However, I do that and it doesn't do anything...getting closer, but seems like I'm still far away...
    Last edited by CodeLiftsleep; 08-10-2016 at 1:14 PM.

  2. #22
    CodeLiftsleep is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.5
    Join Date
    Mar 2016
    Posts
    20
    Reputation
    15
    FINALLY!!!! Got it working---I called Prog.Dispatcher.BeginInvoke(Sub() LoopPercent(byval x as integer, byval MyVM as ProgressBarDialogViewmodel) inside the player generation loop...don't have it bound via UI, but its working by directly updating the progress.Value inside the Loop...now I need to figure out how to get the binding to work properly...

    Code:
        Private Sub GenNewPlayersASync(ByVal MyVM As ProgressBarDialogViewModel, ByVal Prog As ProgressBarDialog)
    
    
            Dim x As Integer = 0
            'Generate the Players on an Async Thread
            Console.WriteLine($"This is the current thread 4: {System.Threading.Thread.CurrentThread.ManagedThreadId}")
            Console.WriteLine($"Progress Bar Has Access? {Prog.Dispatcher.CheckAccess()}")
    
    
            For x = 1 To MyVM.TotalPlayers
                CollegePlayers.GenDraftPlayers(x, MyDraft, DraftDT, DraftClass, PosCount)
                Dim i As Integer = x
                MyVM.PlayerCount = i
                If MyVM.PlayerCount > 1 And Not Prog.Dispatcher.CheckAccess() Then
                    Prog.Dispatcher.BeginInvoke(Sub() Prog.LoopPercent(x, MyVM))
                End If
            Next x
        End Sub
    Inside the ProgressBar Model itself:

    Code:
        Public Sub LoopPercent(ByVal x As Integer, ByVal MyVM As ProgressBarDialogViewModel)
            progress.Value = CInt(((x / MyVM.TotalPlayers) * 100))
        End Sub
    Damn...2 days but I got it figured out!
    Last edited by CodeLiftsleep; 08-10-2016 at 2:24 PM.

Page 2 of 2 FirstFirst 12

Tags for this Thread

Bookmarks

Posting Permissions

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