Part 19 - Understanding async and Awaitable Tasks

Sign in to queue

The Discussion

  • User profile image
    arko

    Being accustomed to threads these async-awaits make my head spin.

    In phone 8.0 I used to have something like:

    Private Sub MainPage_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
      Dim theThread As New System.Threading.Thread(AddressOf DoSlowWork)
      theThread.IsBackground = True
      theThread.Start(New MyParametersBundledClass With {.FileToOpen = "MyDir/MyFile.zip"})
      'Other stuff that needs to run right away before the thread above finishes!
    End Sub
    
    Private Sub DoSlowWork(ParameterBundle As MyParametersBundledClass)
      Dim fs As IO.FileStream = IO.File.OpenRead(ParameterBundle.FileToOpen)
      'Some processing here to populate MyParameters As MyParametersClass...
      Deployment.Current.Dispatcher.BeginInvoke(New Action(Of MyParametersClass)(AddressOf UpdateViewModel), MyParameters)
    End Sub
    Private Sub UpdateViewModel(NewStuff As MyParametersClass))
      MyViewModel.MyObservableCollection.Add(NewStuff.NewItems)
    End Sub

    Now that in phone 8.1 the OpenRead is awaitable I have a feeling I should be doing more changes than just adding Async to all the Subs all the way till MainPage_Loaded; But I have no idea what?

    Dim fs As System.IO.Stream = Await Windows.Storage.ApplicationData.Current.LocalFolder.OpenStreamForRead(ZipPath)

     

  • User profile image
    ecoderie

    Thanks Bob, you made it really easy for me to understand this concept. I learnt a lot from you on channel9 and learnvs.net(devu). you're doing great.

Add Your 2 Cents