WPF VB.Net ProgressBar updating in an Async Method -


i've seen few other posts seem confused since i've seen done several ways , haven't gotten correct of ways, thought ask case can learn doing wrong. after learning , switching c# of programming, vb.net seems clunky in syntax lot of things, lambda , "on-the-fly" functions.

i have long running task football game working on generates players. async method utilizing task.factory.startnew method.

here pertinent code:

  private sub createdraft_click(sender object, e routedeventargs) handles createdraft.click          timeit(sub() reallygennewplayers())    end sub         'uses stopwatch time how long takes , sends console.         private sub timeit(myaction action)             dim sw new stopwatch             sw.start()             myaction()             console.writeline($"total time generating players: {sw.elapsed} seconds")             sw.stop()         end sub           private async sub gennewplayersasync()             dim myvalue = 0             'generate players on async thread             dim x integer              integer = 1 numplayers                 x = 'avoids issue using iteration variable inside delegate                 collegeplayers.gendraftplayers(i, mydraft, draftdt, draftclass, poscount)                  'prog_valuechanged(dbnull.value, new routedpropertychangedeventargs(of double))                  'calls delegate update progress bar avoid having variable locked background thread                  dispatcher.invoke(sub()                                worker.reportprogress((x / numplayers) * 100)                                end sub)             next          end sub          'creates task run player generation code , wait til finished         private sub reallygennewplayers()             dim mytask = task.factory.startnew(sub() gennewplayersasync())             task.waitall(mytask)         end sub 

so here do: have progressbar created in xaml has progress_changed event. have far based on post, issue when have call function inside gennewplayersasync() wants routedpropertychangeeventargs double i'm not sure do...i tried creating 1 using .new(old value, new value) didn't work either , threw error.

public async sub prog_valuechanged(sender object, e routedpropertychangedeventargs(of double))         dim progress = new progress(of integer)(function(percent)                                                     prog.value = e.newvalue                                                     return prog.value                                                 end function)         await task.run(sub() doprocessing(progress))      end sub      public sub doprocessing(progress iprogress(of integer))         dim integer = 0         while <> 100             thread.sleep(100)             ' cpu-bound work             if progress isnot nothing                 progress.report(i)             end if         end while     end sub 

i progressbar bound inotifychanged property , update automatically when value gets changed. none of seems working. ui still unresponsive , when set different parts async, start getting exceptions appears parts of generation algorithm aren't working returning null...very confused of this, , sure answer pretty simple...

if guys give several examples of different ways work can learn different methods , maybe state pros , cons of method appreciate it...


Comments