CommunityToolkit / Graph-Controls

Set of Helpers and Controls for Windows development using the Microsoft Graph.
https://docs.microsoft.com/en-us/windows/communitytoolkit/graph/overview
Other
155 stars 39 forks source link

Race condition issue when subscribing to ProviderStateChanged #124

Closed pekspro closed 3 years ago

pekspro commented 3 years ago

Describe the bug

There is a race condition issue when subscribing to ProviderManager.Instance.ProviderStateChanged. Currently this only works if you do this before ProviderManager.Instance.GlobalProvider is setup.

Steps to Reproduce

It is easy to see this in the UwpMsalProviderSample project. Change the constructor of LoginButton to this:

    public LoginButton()
    {
        InitializeComponent();

        _ = Task.Run(async () =>
        {
            await Task.Delay(2000);

            _ = Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
           {
               ProviderManager.Instance.ProviderStateChanged += (s, e) => UpdateState();
               UpdateState();
           });
        });
    }

Now when the application is executed the text on the sign in button will not be updated.

Expected behavior

It should not matter when you subscribe to ProviderManager.Instance.ProviderStateChanged.

Additional context

I will create an PR for this.