0

I just got to some bug in .NET I think, when I start a synchronous process in event of preview mouse down it causes mouse up events to show before all the mouse down events that should come after.

when I put the new process in the task it's the right order output:

DockPanel_PreviewMouseLeftButtonDown
Bell_Image_PreviewMouseLeftButtonDown
Bell_Image_MouseLeftButtonDown
DockPanel_MouseLeftButtonDown
DockPanel_PreviewMouseUp
Bell_Image_PreviewMouseUp
Bell_Image_MouseUp
DockPanel_MouseUp

but when start process synchronously in "DockPanel_PreviewMouseUp" the output is:

DockPanel_PreviewMouseLeftButtonDown
DockPanel_PreviewMouseUp
Bell_Image_PreviewMouseUp
Bell_Image_MouseUp
DockPanel_MouseUp
Bell_Image_PreviewMouseLeftButtonDown
Bell_Image_MouseLeftButtonDown
DockPanel_MouseLeftButtonDown

Why this is happening? thanks

the code that causes the order of the messed-up events:

XAML:

<DockPanel MouseLeftButtonDown="DockPanel_MouseLeftButtonDown" PreviewMouseLeftButtonDown="DockPanel_PreviewMouseLeftButtonDown" MouseUp="DockPanel_MouseUp" PreviewMouseUp="DockPanel_PreviewMouseUp">
            <Grid>
                <Image x:Name="Bell_Image" Source="Bell.png" PreviewMouseLeftButtonDown="Bell_Image_PreviewMouseLeftButtonDown" MouseLeftButtonDown="Bell_Image_MouseLeftButtonDown" MouseUp="Bell_Image_MouseUp" PreviewMouseUp="Bell_Image_PreviewMouseUp"/>
            </Grid>
 </DockPanel> 

code behind C#:

        private void Bell_Image_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Console.WriteLine("Bell_Image_PreviewMouseLeftButtonDown");
        }

        private void Bell_Image_MouseUp(object sender, MouseButtonEventArgs e)
        {
            Console.WriteLine("Bell_Image_MouseUp");
        }

        private void DockPanel_MouseUp(object sender, MouseButtonEventArgs e)
        {
            Console.WriteLine("DockPanel_MouseUp");
        }

        private void DockPanel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Console.WriteLine("DockPanel_MouseLeftButtonDown");
        }

        private void Bell_Image_PreviewMouseUp(object sender, MouseButtonEventArgs e)
        {
            Console.WriteLine("Bell_Image_PreviewMouseUp");
        }

        private void DockPanel_PreviewMouseUp(object sender, MouseButtonEventArgs e)
        {
            Console.WriteLine("DockPanel_PreviewMouseUp");
        }

        private void Bell_Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Console.WriteLine("Bell_Image_MouseLeftButtonDown");
        }

        private void DockPanel_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Console.WriteLine("DockPanel_PreviewMouseLeftButtonDown");
            Process.Start("Some_File.pdf");
        }

I expected it to not mess with the order of the events, interested to know if its a bug or if there is an explanation for that

yo245
  • 1
  • 1
  • Same cause as [this one](https://stackoverflow.com/questions/43210194/windowsform-mouseup-fires-twice-when-using-process-start), same workaround. Do beware that this code will no longer work when you port it to .NET5+, ProcessStartInfo.UseShellExecute is so troublesome that they changed the default. – Hans Passant Apr 10 '23 at 10:52
  • @HansPassant I tried this also in .NET6 with the ProcessStartInfo and so and the same happens – yo245 Apr 10 '23 at 10:56
  • Of course, tinkering with ProcessStartInfo is not the solution. Use Dispatcher.BeginInvoke() instead. – Hans Passant Apr 10 '23 at 11:01
  • @HansPassant i dont think it's the same bug, also if I put some prints after the Process. Start they printed after the mouse-up events and then the mouse-down events happen, very strange it's like after the Process. Start it moves to the up events immediately and then return to the function the solution is as i mention to run it asynchronously with test – yo245 Apr 10 '23 at 11:02

1 Answers1

0

It seems like after Process.Start the UI thread is fired all the events that wait in the dispatcher, why this happens haha?

like if after Process.Start("Some_File.pdf"); I will put some prints they will be printed after the mouse-up events if I change the function that starts the process to this:

private void DockPanel_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    Console.WriteLine("DockPanel_PreviewMouseLeftButtonDown");
    Process.Start("Some_File.pdf");
Console.WriteLine("check1");
Console.WriteLine("check2");
}

the output will be:

DockPanel_PreviewMouseLeftButtonDown
DockPanel_PreviewMouseUp
Bell_Image_PreviewMouseUp
Bell_Image_MouseUp
DockPanel_MouseUp
check1
check2
Bell_Image_PreviewMouseLeftButtonDown
Bell_Image_MouseLeftButtonDown
DockPanel_MouseLeftButtonDown
yo245
  • 1
  • 1