Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ChildWindow Close button event handler executes multiple times if it fast clicked many times

My ChildWindow has CloseButton and handler assigned to Click event. Code (only for example):

Declaring close button:

<Button x:Name="CloseButton" Click="OnCloseButtonClick" />

Private counter (for diagnostics problem):

private uint _i;

Close event handler:

OnCloseButtonClick(object sender, RoutedEventArgs e)
{
     DialogResult = true;
     System.Diagnostics.Debug(_i++);
}

After fast clicking program can output "1", "2", "3", and so on... As i know after setting DialogResult = true(or false), ChildWindow should be closed and there should not be any way to raise the CloseButton's Click event second time.

Can anyone help me to figure out cause of the problem and help to solve it without bool flags (executed/!executed)?

like image 389
Dzmitry Martavoi Avatar asked Nov 04 '22 10:11

Dzmitry Martavoi


1 Answers

Copied from my comments

The problem is because the close activates the close window animation to close the window which means the button events are available until the animation finishes and the window is closed, see During Silverlight ChildWindow closing animation user can click any button

A quick and dirty solution is to add

((Button)sender).IsEnabled=false;

To the beginning of you click handler function.

like image 133
Bob Vale Avatar answered Nov 13 '22 04:11

Bob Vale