locked
How to create synchronous dialog messages ? RRS feed

  • Question

  • I want to display 2 consecutive messages A and B in this order. The code is :

    ...

    MsgBox("A")

    MsgBox("B")

    ...

    Public Async Sub MsgBox(msg$)
            Dim dialog = New Windows.UI.Popups.MessageDialog(msg$)
            Await dialog.ShowAsync()
    End Sub

    As we need to use ShowAsync, the result is randomized: sometimes A before B, sometimes B before A.

    I need to use this code :

            Dim dialog = New Windows.UI.Popups.MessageDialog("A")
            Await dialog.ShowAsync()

            dialog = New Windows.UI.Popups.MessageDialog("B")

            Await dialog.ShowAsync()

    Why?

    Thursday, October 15, 2015 1:34 PM

Answers

  • >>Why?

    Because you are not awaiting the the first call to the async MsgBox method.

    You could call the Wait() method on the Task (IAsyncOperation) returned by the ShowAsync method to make it synchronous/blocking:

    Public Sub MsgBox(msg$)
             Dim dialog = New Windows.UI.Popups.MessageDialog(msg$)
             dialog.ShowAsync().Wait()
    End Sub

    But the reason why you get inconsistent results is that you don't await the call(s) to your async MsgBox method:

    Await MsgBox("A")
    Await MsgBox("B")

    If you don't await the call to MsgBox("A"), the call to MsgBox("B") may be called before the MsgBox method calls the ShowAsync() method.


    Hope that helps

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    Thursday, October 15, 2015 2:41 PM

All replies

  • >>Why?

    Because you are not awaiting the the first call to the async MsgBox method.

    You could call the Wait() method on the Task (IAsyncOperation) returned by the ShowAsync method to make it synchronous/blocking:

    Public Sub MsgBox(msg$)
             Dim dialog = New Windows.UI.Popups.MessageDialog(msg$)
             dialog.ShowAsync().Wait()
    End Sub

    But the reason why you get inconsistent results is that you don't await the call(s) to your async MsgBox method:

    Await MsgBox("A")
    Await MsgBox("B")

    If you don't await the call to MsgBox("A"), the call to MsgBox("B") may be called before the MsgBox method calls the ShowAsync() method.


    Hope that helps

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    Thursday, October 15, 2015 2:41 PM
  • Thanks for your quick reply!

    That you propose is not totally accepted by VB (VS 2015) for Universal apps because .Wait() is not a member.

    Anyway, this is solved by the following code, thanks to 'Await Msgbox' (that you suggested) and 'As Task' in the function MsgBox.

    We may also use Sub Async BT_Test_Click without using the sub Test.

    Private Sub BT_Test_Click(sender As Object, e As RoutedEventArgs) Handles BT_Test.Click
            Test()
    End Sub

    Async Sub Test()
            Await MsgBox("1")
            Await MsgBox("2")
    End Sub

    Public Async Function MsgBox(msg$) As Task
          Dim dialog = New Windows.UI.Popups.MessageDialog(msg$)
          Await dialog.ShowAsync()
    End Function

    So, not totally easy to understand and solve the asynchronous/synchronous methods !

    Thursday, October 15, 2015 5:09 PM