question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Exception when attempting to show window after closing it via title bar

See original GitHub issue

Describe the bug

When running H.NotifyIcon.Apps.WinUI, after closing the window via the X icon in the title bar, attempting to show the window via the taskbar icon results in an exception.

Specifically, System.Runtime.InteropServices.COMException: 'The operation identifier is not valid. (0x800710DD)' when accessing window.Visible in ShowHideWindowCommand_ExecuteRequested.

Steps to reproduce the bug

  1. Run H.NotifyIcon.Apps.WinUI
  2. Close the window via the title bar X icon
  3. Left click the taskbar icon or right click and select “show/hide window”
  4. Exception occurs

Expected behavior

The window should appear.

Screenshots

image

NuGet package version

No response

Platform

WinUI

IDE

Visual Studio 2022

Windows Version

Windows 11

WindowsAppSDK Version

1.2

WindowsAppSDK Type

Packaged

Manifest

No response

Additional context

No response

Issue Analytics

  • State:closed
  • Created 9 months ago
  • Comments:5

github_iconTop GitHub Comments

2reactions
JasonWei512commented, Jan 11, 2023

@myfix16

MainWindow.Closed += MainWindow_Closed;

private void MainWindow_Closed(object sender, WindowEventArgs args)
{
    args.Handled = true;
}
1reaction
JasonWei512commented, Dec 28, 2022

Use a DispatcherQueue to execute UI related code on the main UI thread. Otherwise you may get an exception.

I recommend using the CommunityToolkit.WinUI nuget package’s DispatcherQueueExtensions.EnqueueAsync.

using CommunityToolkit.WinUI;

class MyClass
{
    // Get a DispatcherQueue instance for later use. This has to be called on the UI thread,
    // but it can then be cached for later use and accessed from a background thread as well.
    private readonly DispatcherQueue dispatcherQueue = DispatcherQueue.GetForCurrentThread();

    // ...

    // Your tray icon event handler. This may not be called from the main UI thread
    private async void ExitApplicationCommand_ExecuteRequested(object? sender, EventArgs args)
    {
        // Execute some code on the target dispatcher queue
        await dispatcherQueue.EnqueueAsync(() =>
        {
            if (App.MainWindow.Visible)
            {
                // ...
            }
        });
    }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

WPF: Cannot reuse window after it has been closed
When we try to show the Window which is closed, we will get the following exception. "Cannot set Visibility or call Show, ShowDialog, ......
Read more >
Manage app windows (Windows App SDK)
Some of the benefits of using AppWindow (even when working with a UI framework) are: Easy title bar customization; which by default maintains ......
Read more >
Python close window. close () method whenever we want ...
Show file. … The simplest way to close a window is to click the right (Windows) or left (macOS) 'X' button on the...
Read more >
How to Make Frames (Main Windows)
A Frame is a top-level window with a title and a border. The size of the frame includes ... See Responding to Window-Closing...
Read more >
How to Make Dialogs (The Java™ Tutorials > Creating ...
A Dialog window is an independent sub window meant to carry temporary notice apart from the main Swing Application Window. Most Dialogs present...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found