none
[UWP][ContentDialog] Using ContentDialog in UWP class (Dll file)?

    Question

  • How do I get a contentdialog to display in from a dll file? I have a need to display a error message with the error icon.

    Here's the code that calls a function to display a messagebox:

      ' Dim a As Task(Of Boolean) = DisplayMessageBox(errormsg)
                            ' a.Wait()

    Here's the actual function code:

           Private Async Function DisplayMessageBox(errormsg As String) As Task(Of Boolean)
                If errormsg <> "" Then
                    Dim title = New TextBlock()
                    title.Inlines.Add(New Documents.Run() With
    {
        .Text = ChrW(&HE783).ToString(),
        .FontFamily = New FontFamily("Segoe MDL2 Assets")
    })
                    title.Inlines.Add(New Documents.Run() With
    {
        .Text = errormsg
    })
    
                    Dim dialog = New ContentDialog() With
    {
    .Title = title,
    .PrimaryButtonText = "OK",
    .HorizontalAlignment = HorizontalAlignment.Center,
    .VerticalAlignment = VerticalAlignment.Center
    }
                    Dim a As IAsyncOperation(Of ContentDialogResult) = dialog.ShowAsync
                    a.AsTask.Wait()
                    Return True
                Else
                    Return False
                End If
    
            End Function


    Once you eliminate the impossible, whatever remains, no matter how improbable, must be the truth. - Sherlock Holmes. speak softly and carry a big stick - theodore roosevelt. Fear leads to anger, anger leads to hate, hate leads to suffering - Yoda. Blog - http://www.computerprofessions.us

    Wednesday, April 05, 2017 10:44 PM

All replies

  • Hello The Thinker,

    To make a general contentdialog shows I modified some of your code and here is the result:

    The calling code:

     Private Async Sub Button_Click(sender As Object, e As RoutedEventArgs)
            Dim TestClass As New ClassLibrary1.Class1
            Await TestClass.DisplayMessageBox("test")
        End Sub

    The code in a classlibrary:

     Public Async Function DisplayMessageBox(errormsg As String) As Task(Of Boolean)
            If errormsg <> "" Then
                Dim title = New TextBlock()
                title.Inlines.Add(New Documents.Run() With
    {
    .Text = ChrW(&HE783).ToString(),
    .FontFamily = New FontFamily("Segoe MDL2 Assets")
    })
                title.Inlines.Add(New Documents.Run() With
    {
    .Text = errormsg
    })
    
                Dim dialog = New ContentDialog() With
    {
    .Title = title,
    .PrimaryButtonText = "OK",
    .HorizontalAlignment = HorizontalAlignment.Center,
    .VerticalAlignment = VerticalAlignment.Center
    }
                Dim a As ContentDialogResult = Await dialog.ShowAsync()
    
                Return True
            Else
                Return False
            End If
        End Function
    

    I've modified the code about the async and await part. In your code when your use a.AsTask.Wait() it is actually a sync process. See here for details about Task.Wait https://msdn.microsoft.com/en-us/library/dd235606(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2 This makes the problem. I suspect the thread is blocked here.

    Best regards,

    Barry



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 06, 2017 9:48 AM
    Moderator
  • Hello The Thinker,

    To make a general contentdialog shows I modified some of your code and here is the result:

    The calling code:

     Private Async Sub Button_Click(sender As Object, e As RoutedEventArgs)
            Dim TestClass As New ClassLibrary1.Class1
            Await TestClass.DisplayMessageBox("test")
        End Sub

    The code in a classlibrary:

     Public Async Function DisplayMessageBox(errormsg As String) As Task(Of Boolean)
            If errormsg <> "" Then
                Dim title = New TextBlock()
                title.Inlines.Add(New Documents.Run() With
    {
    .Text = ChrW(&HE783).ToString(),
    .FontFamily = New FontFamily("Segoe MDL2 Assets")
    })
                title.Inlines.Add(New Documents.Run() With
    {
    .Text = errormsg
    })
    
                Dim dialog = New ContentDialog() With
    {
    .Title = title,
    .PrimaryButtonText = "OK",
    .HorizontalAlignment = HorizontalAlignment.Center,
    .VerticalAlignment = VerticalAlignment.Center
    }
                Dim a As ContentDialogResult = Await dialog.ShowAsync()
    
                Return True
            Else
                Return False
            End If
        End Function

    I've modified the code about the async and await part. In your code when your use a.AsTask.Wait() it is actually a sync process. See here for details about Task.Wait https://msdn.microsoft.com/en-us/library/dd235606(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2 This makes the problem. I suspect the thread is blocked here.

    Best regards,

    Barry



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    I know you are trying to help but the problem is both code points are directly from a dll file. I need to display an error message during the execution of a method inside the function in the dll file. The only exception is if I have a method to redirect error messages to the GUI side.

    Once you eliminate the impossible, whatever remains, no matter how improbable, must be the truth. - Sherlock Holmes. speak softly and carry a big stick - theodore roosevelt. Fear leads to anger, anger leads to hate, hate leads to suffering - Yoda. Blog - http://www.computerprofessions.us


    • Edited by The Thinker Thursday, April 06, 2017 10:01 PM
    Thursday, April 06, 2017 9:49 PM
  • @The Thinker,

    1. What do you mean by from a DLL?(Am I right on putting the code in a universal class library?)

    2. What is the error you get?

    3. What does this "both code points are directly from a dll file" means? Both error info and content dialog?

    And by the way we want to know whether your code is run on the dispatcher thread, see the doc here:

    https://docs.microsoft.com/en-us/uwp/api/windows.ui.core.coredispatcher

    When I was testing I used a class library and I can see the content dialog hangs and it will not popup. So I modified to make it works. But as it does not make sense for you, could you please clairfy more for us?

    Best regards,

    Barry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Friday, April 07, 2017 2:04 AM
    Moderator
  • @The Thinker,

    1. What do you mean by from a DLL?(Am I right on putting the code in a universal class library?)

    2. What is the error you get?

    3. What does this "both code points are directly from a dll file" means? Both error info and content dialog?

    And by the way we want to know whether your code is run on the dispatcher thread, see the doc here:

    https://docs.microsoft.com/en-us/uwp/api/windows.ui.core.coredispatcher

    When I was testing I used a class library and I can see the content dialog hangs and it will not popup. So I modified to make it works. But as it does not make sense for you, could you please clairfy more for us?

    Best regards,

    Barry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    For question #1, you hit it right on the mark. It is in a universal class library.

    For question #2, most of the errors should be a no connection error message or a processing error message (unlikely though if I have a good connection).

    For question #3, I am having both of the code points above in a universal class library as most of the errors will occur within it. I thinking this is an issue of blocking the thread before it gets the chance to display it. Can I activate the blocked thread?


    Once you eliminate the impossible, whatever remains, no matter how improbable, must be the truth. - Sherlock Holmes. speak softly and carry a big stick - theodore roosevelt. Fear leads to anger, anger leads to hate, hate leads to suffering - Yoda. Blog - http://www.computerprofessions.us

    Saturday, April 08, 2017 4:05 PM
  • Hi The Thinker,

    >>Can I activate the blocked thread?

    Problem 1: Dispatcher thread

    Depending on where you call the method DisplayMessageBox(), the thread may different. To make the ContentDialog show we need to call it from UI thread.

    You can see my code. It is called from Button Click so I'm already on UI thread.

    However if you are not calling it from UI thread you need to use the dispatcher thread as I mentioned in my last post:

    Await dispatcher.RunAsync(CoreDispatcherPriority.Normal, Function() 
    //Put your things here
    End Function)
    

    Problem 2:

    If you already on a dispatcher thread and then calling this line of code: "AsTask.Wait" could hang or throw.

    Now the problem is that we still have no idea about how you called your DisplayMessageBox method so it's a little hard for us to help you troubleshoot the problem. We are lack of info here.

    Best regards,

    Barry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by The Thinker Tuesday, April 18, 2017 9:40 PM
    • Unmarked as answer by The Thinker Tuesday, April 18, 2017 10:59 PM
    Friday, April 14, 2017 3:04 AM
    Moderator
  • Hi The Thinker,

    >>Can I activate the blocked thread?

    Problem 1: Dispatcher thread

    Depending on where you call the method DisplayMessageBox(), the thread may different. To make the ContentDialog show we need to call it from UI thread.

    You can see my code. It is called from Button Click so I'm already on UI thread.

    However if you are not calling it from UI thread you need to use the dispatcher thread as I mentioned in my last post:

    Await dispatcher.RunAsync(CoreDispatcherPriority.Normal, Function() 
    //Put your things here
    End Function)

    Problem 2:

    If you already on a dispatcher thread and then calling this line of code: "AsTask.Wait" could hang or throw.

    Now the problem is that we still have no idea about how you called your DisplayMessageBox method so it's a little hard for us to help you troubleshoot the problem. We are lack of info here.

    Best regards,

    Barry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    turns out you were right and your last code post worked when the second part was called from the UI thread. So, it has to deal with blocking specifically. I will try out your dispatcher thread idea and get back with you.

    Once you eliminate the impossible, whatever remains, no matter how improbable, must be the truth. - Sherlock Holmes. speak softly and carry a big stick - theodore roosevelt. Fear leads to anger, anger leads to hate, hate leads to suffering - Yoda. Blog - http://www.computerprofessions.us

    Friday, April 14, 2017 11:35 AM
  • Hi The Thinker,

    >>Can I activate the blocked thread?

    Problem 1: Dispatcher thread

    Depending on where you call the method DisplayMessageBox(), the thread may different. To make the ContentDialog show we need to call it from UI thread.

    You can see my code. It is called from Button Click so I'm already on UI thread.

    However if you are not calling it from UI thread you need to use the dispatcher thread as I mentioned in my last post:

    Await dispatcher.RunAsync(CoreDispatcherPriority.Normal, Function() 
    //Put your things here
    End Function)

    Problem 2:

    If you already on a dispatcher thread and then calling this line of code: "AsTask.Wait" could hang or throw.

    Now the problem is that we still have no idea about how you called your DisplayMessageBox method so it's a little hard for us to help you troubleshoot the problem. We are lack of info here.

    Best regards,

    Barry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Turns out I found out about code which seems to solve the problem your talking about here but I am going to test it:

    https://github.com/Microsoft/Windows-task-snippets/blob/master/tasks/UI-thread-task-await-from-background-thread.md

    Its supposed to wait until the message box completes before moving on in the program. The page you borrowed that from says the program will continue regardless of if I click the messagebox or dialog especially if I use your code.


    Once you eliminate the impossible, whatever remains, no matter how improbable, must be the truth. - Sherlock Holmes. speak softly and carry a big stick - theodore roosevelt. Fear leads to anger, anger leads to hate, hate leads to suffering - Yoda. Blog - http://www.computerprofessions.us

    Friday, April 14, 2017 12:20 PM
  • @Barry Wang,

    https://social.msdn.microsoft.com/Forums/windowsapps/en-US/9d917522-9306-4eb0-bd7f-3bde27ff7243/uwpprogressbaruniversal-class-libraryhow-to-share-progress-data-from-universal-class-library?forum=wpdevelop

     The Thinker seems to around the same type of question to post new thread on the forum.

    Monday, April 17, 2017 5:42 AM
  • @Barry Wang,

    https://social.msdn.microsoft.com/Forums/windowsapps/en-US/9d917522-9306-4eb0-bd7f-3bde27ff7243/uwpprogressbaruniversal-class-libraryhow-to-share-progress-data-from-universal-class-library?forum=wpdevelop

     The Thinker seems to around the same type of question to post new thread on the forum.


    Similar but not the same question with the exception that the code from this post will be using dispatcher.runasync. Note: originally I said both will but this is not necessary.

    Once you eliminate the impossible, whatever remains, no matter how improbable, must be the truth. - Sherlock Holmes. speak softly and carry a big stick - theodore roosevelt. Fear leads to anger, anger leads to hate, hate leads to suffering - Yoda. Blog - http://www.computerprofessions.us


    • Edited by The Thinker Tuesday, April 18, 2017 9:39 PM
    Monday, April 17, 2017 9:24 PM
  • Hi The Thinker,

    >>Can I activate the blocked thread?

    Problem 1: Dispatcher thread

    Depending on where you call the method DisplayMessageBox(), the thread may different. To make the ContentDialog show we need to call it from UI thread.

    You can see my code. It is called from Button Click so I'm already on UI thread.

    However if you are not calling it from UI thread you need to use the dispatcher thread as I mentioned in my last post:

    Await dispatcher.RunAsync(CoreDispatcherPriority.Normal, Function() 
    //Put your things here
    End Function)
    

    Problem 2:

    If you already on a dispatcher thread and then calling this line of code: "AsTask.Wait" could hang or throw.

    Now the problem is that we still have no idea about how you called your DisplayMessageBox method so it's a little hard for us to help you troubleshoot the problem. We are lack of info here.

    Best regards,

    Barry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    This works after some playing around but it requires the code I linked to below to work effectively.

    Once you eliminate the impossible, whatever remains, no matter how improbable, must be the truth. - Sherlock Holmes. speak softly and carry a big stick - theodore roosevelt. Fear leads to anger, anger leads to hate, hate leads to suffering - Yoda. Blog - http://www.computerprofessions.us

    Tuesday, April 18, 2017 9:40 PM
  • Hi The Thinker,

    >>Can I activate the blocked thread?

    Problem 1: Dispatcher thread

    Depending on where you call the method DisplayMessageBox(), the thread may different. To make the ContentDialog show we need to call it from UI thread.

    You can see my code. It is called from Button Click so I'm already on UI thread.

    However if you are not calling it from UI thread you need to use the dispatcher thread as I mentioned in my last post:

    Await dispatcher.RunAsync(CoreDispatcherPriority.Normal, Function() 
    //Put your things here
    End Function)
    

    Problem 2:

    If you already on a dispatcher thread and then calling this line of code: "AsTask.Wait" could hang or throw.

    Now the problem is that we still have no idea about how you called your DisplayMessageBox method so it's a little hard for us to help you troubleshoot the problem. We are lack of info here.

    Best regards,

    Barry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    I noticed that I have the wrong dispatcher. How do I get the correct one to display from another non-ui thread? I am doing this from a class library so it is not executed on the UI thread. Any help on this problem? It seems to work from a module/class inside the UI code side.

    Once you eliminate the impossible, whatever remains, no matter how improbable, must be the truth. - Sherlock Holmes. speak softly and carry a big stick - theodore roosevelt. Fear leads to anger, anger leads to hate, hate leads to suffering - Yoda. Blog - http://www.computerprofessions.us

    Tuesday, April 18, 2017 11:00 PM
  • @The Thinker,

    Hmm, right, the article you showed can fully answered your question here. You can mark it as the answer to benefit other members. It has a good explanation.

    Best regards,

    Barry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Friday, April 21, 2017 2:43 PM
    Moderator
  • @The Thinker,

    Another thing. What do you mean by saying "wrong dispatcher"? I find that this is your last reply so it seems you still have problem.

    Actually we recommend you choose another way to do this:

    Make your class in the DLL raise an event that there was an error. The main app can listen for the event and show the ContentDialog in response to the event (it’ll still need to be dispatched to the UI thread).

    This separate the logic from UI better.

    Best regards,

    Barry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    9 hours 34 minutes ago
    Moderator