Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[UWP Migration] Dispatcher to DispatcherQueue #7

Open
michael-hawker opened this issue Jul 11, 2024 · 0 comments
Open

[UWP Migration] Dispatcher to DispatcherQueue #7

michael-hawker opened this issue Jul 11, 2024 · 0 comments

Comments

@michael-hawker
Copy link
Owner

Related to #6

See:

It's not widely known that Dispatcher is effectively deprecated (not sure why it hasn't been marked as such) and that it's better to use DispatcherQueue instead. We made this change in the Windows Community Toolkit a while ago now. It's well supported and it means a seemlesss transition for those pieces to the Windows App SDK and WinUI 3.

We should have an analyzer and codefix which detects the old usage and swaps it out for DispatcherQueue instead.

We'd probably need to inject a private variable and set in the constructor to ensure the most compatible fix.

// MainPage.xaml.cs in a UWP app
public void NotifyUser(string strMessage)
{
    if (this.Dispatcher.HasThreadAccess)
    {
        StatusBlock.Text = strMessage;
    }
    else
    {
        var task = this.Dispatcher.RunAsync(
            Windows.UI.Core.CoreDispatcherPriority.Normal,
            () => StatusBlock.Text = strMessage);
    }
}

Expected:

// MainPage.xaml.cs in a Windows App SDK app
public void NotifyUser(string strMessage)
{
    if (this.DispatcherQueue.HasThreadAccess)
    {
        StatusBlock.Text = strMessage;
    }
    else
    {
        bool isQueued = this.DispatcherQueue.TryEnqueue(
        Microsoft.UI.Dispatching.DispatcherQueuePriority.Normal,
        () => StatusBlock.Text = strMessage);
    }
}

Should handle both the DP usage of Dispatcher as well as the CoreWindow general one.

Should also handle more complex cases where the containing type doesn't have a DispatcherQueue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant