wpf icon indicating copy to clipboard operation
wpf copied to clipboard

window events are invalid and cannot be closed or clicked

Open hzh1993 opened this issue 3 months ago • 2 comments

Description

The non-modal window is closed as the owner. Other window events are invalid and cannot be closed or clicked.

Reproduction Steps

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    Window1 window = null;
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        window = new Window1();
        window.Show();
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        Window1 window2 = new Window1();
        window2.Owner = window;

        Task.Run(() => {
            Thread.Sleep(3000);
            System.Windows.Application.Current.Dispatcher.BeginInvoke((System.Action)(() =>
            {
                window.Close();  //Causes other window events to invalid
            }));
        });

        window2.ShowDialog();
    }
}

Expected behavior

Works properly

Actual behavior

Causes other window events to invalid

Regression?

This problem exists testing with .Net 5 or .Net 9

Known Workarounds

No response

Impact

No response

Configuration

No response

Other information

No response

hzh1993 avatar Sep 04 '25 09:09 hzh1993

It is seems the old bug. I can repro this issue by the mini step:

MainWindow.xaml:

    <Grid>
        <Button HorizontalAlignment="Center" VerticalAlignment="Center" Content="The Button" Click="Button_OnClick"></Button>
    </Grid>

MainWindow.xaml.cs:

    private void Button_OnClick(object sender, RoutedEventArgs e)
    {
        var window1 = new Window()
        {
            Title = "Windows1"
        };
        window1.Show();

        Window window2 = new Window
        {
            Title = "Windows2",
            Owner = window1,
        };

        Task.Run(async () =>
        {
            await Task.Delay(1000);
            await Dispatcher.InvokeAsync(() =>
            {
                Debug.Assert(window1 != null, nameof(window1) + " != null");
                window1.Close();  //Causes other window events to invalid
            });
        });

        window2.ShowDialog();
    }

You can find all my code in https://github.com/lindexi/lindexi_gd/tree/d8b0a7015f1cb114f2c67a62b19f14d12f8fc8c2/WPFDemo/DeelagojawweFakejachebelrair

You can repro this issue by run the code and click The Button button.

lindexi avatar Sep 04 '25 12:09 lindexi

The stack trace is:

 	WindowsBase.dll!MS.Win32.UnsafeNativeMethods.GetMessageW(ref System.Windows.Interop.MSG msg, System.Runtime.InteropServices.HandleRef hWnd, int uMsgFilterMin, int uMsgFilterMax)
 	WindowsBase.dll!System.Windows.Threading.Dispatcher.GetMessage(ref System.Windows.Interop.MSG msg, nint hwnd, int minMessage, int maxMessage)
 	WindowsBase.dll!System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame frame = {System.Windows.Threading.DispatcherFrame})
 	PresentationFramework.dll!System.Windows.Window.ShowHelper(object booleanBox)
 	PresentationFramework.dll!System.Windows.Window.Show()
 	PresentationFramework.dll!System.Windows.Window.ShowDialog()
>	DeelagojawweFakejachebelrair.dll!DeelagojawweFakejachebelrair.MainWindow.Button_OnClick(object sender = {System.Windows.Controls.Button}, System.Windows.RoutedEventArgs e = {System.Windows.RoutedEventArgs})	C#

The Button_OnClick method still wait the Dialog exit.

lindexi avatar Sep 04 '25 12:09 lindexi