Why does the Widget Sample create a duplicate empty instance after a system restart?

Ser Fed 0 Reputation points
2026-01-14T10:27:20.15+00:00

Hello everyone,

I am currently learning Windows 11 Widget development using the C# Windows App SDK. I have built, packaged, and deployed the official Weather Widget sample (from the WindowsAppSDK-Samples repository).

The Issue: I am facing a lifecycle/state management issue with the sample code:

  1. I pin the widget to the Widgets Board. It works correctly.

After restarting the PC (or the Widgets process), the widget remains on the board as expected.

However, the system seems to lose track of the instance state: the "Pin" (add) button becomes available again for this provider.

If I click "Pin" again, it duplicates the widget, but the new instance opens as an empty/blank window and does not load content.

My Question: Is there a specific logic missing in the sample's WidgetProvider.cs (specifically in the CreateWidget or Activate methods) that handles the restoration of existing WidgetIds after a reboot?

How can I modify the provider logic to prevent this duplication and correctly resume the existing widget instance?

Environment:

Windows 11

Visual Studio 2022

Project type: C# Console packaged (Windows App SDK)

Any advice would be appreciated. Thanks!

windows-11-widgets, csharp, winui

Windows development | Windows App SDK
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Q&A Assist
    2026-01-14T10:27:39.3366667+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    The issue you're experiencing with the Weather Widget sample creating a duplicate empty instance after a system restart is likely related to how the widget's state is managed during the lifecycle of the application. Specifically, you need to ensure that the existing widget instances are correctly restored when the system restarts.

    To address this, you should implement logic in the WidgetProvider.cs file to check for existing widget instances during the initialization phase. Here are some steps to consider:

    1. Initialize Running Widgets: When your widget provider is initialized, use the WidgetManager.GetDefault method to get the default widget manager instance and call GetWidgetInfos. This will return an array of WidgetInfo objects that represent the currently running widgets. You should copy the widget IDs and other relevant state information into your provider's state management structure.
    2. Handle Widget Activation: In the Activate method, check if the widget ID already exists in your state management structure. If it does, you should restore the existing instance instead of creating a new one. This will prevent the duplication of the widget.
    3. Update Widget State: Ensure that the widget's state is updated correctly when it is activated or when the system state changes. This includes handling any necessary updates to the widget's data or UI based on the current state.

    By implementing these changes, you should be able to prevent the duplication of the widget and ensure that the existing instance is resumed correctly after a system restart.


    References:

    0 comments No comments

  2. Michael Le (WICLOUD CORPORATION) 10,475 Reputation points Microsoft External Staff Moderator
    2026-01-15T10:04:52.8266667+00:00

    Hello @Ser Fed ,

    By all means, the sample was intended to demonstrate a simple implementation of the widget, so it is not fully optimized for user experience. If the widget process or system restarts, your provider will lose track of the widget IDs and their states.

    Handling state persistence would increase the complexity of the sample, which is likely why it was not included. However, I understand your point about the user experience.

    If you want to manage state for the widget, you can consider implementing local storage or using application settings to save the state when the widget is closed and restore it when it is reopened.

    If you want to improve the sample, you could consider reporting to the official GitHub sample repository: https://github.com/microsoft/WindowsAppSDK-Samples/issues.

    While this is a non-Microsoft link, it is the official Github repository of Windows App SDK samples.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.