为什么在PreviewMouseDown事件中以同步的方式启动一个新的进程,打乱了事件的顺序?(鼠标向上显示在鼠标向下事



我想我刚刚遇到了。net中的一些bug,当我启动一个同步进程来预览鼠标下拉事件时,它会导致鼠标上拉事件在所有鼠标下拉事件之前显示。

当我将新流程放入任务时,它是正确的顺序输出:

DockPanel_PreviewMouseLeftButtonDown
Bell_Image_PreviewMouseLeftButtonDown
Bell_Image_MouseLeftButtonDown
DockPanel_MouseLeftButtonDown
DockPanel_PreviewMouseUp
Bell_Image_PreviewMouseUp
Bell_Image_MouseUp
DockPanel_MouseUp

但是当同步启动进程时在DockPanel_PreviewMouseUp"输出为:

DockPanel_PreviewMouseLeftButtonDown
DockPanel_PreviewMouseUp
Bell_Image_PreviewMouseUp
Bell_Image_MouseUp
DockPanel_MouseUp
Bell_Image_PreviewMouseLeftButtonDown
Bell_Image_MouseLeftButtonDown
DockPanel_MouseLeftButtonDown

为什么会这样?由于

导致混乱事件顺序的代码:

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> 

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");
}

我希望它不会打乱事件的顺序,有兴趣知道这是一个bug还是有一个解释

似乎在Process之后。启动UI线程会触发所有在调度程序中等待的事件,为什么会发生这种情况哈哈?

like if after Process.Start("Some_File.pdf");我将打印一些图片,它们将在鼠标上浮事件后打印如果我将启动该过程的函数更改为:

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

的输出将是:

DockPanel_PreviewMouseLeftButtonDown
DockPanel_PreviewMouseUp
Bell_Image_PreviewMouseUp
Bell_Image_MouseUp
DockPanel_MouseUp
check1
check2
Bell_Image_PreviewMouseLeftButtonDown
Bell_Image_MouseLeftButtonDown
DockPanel_MouseLeftButtonDown

相关内容

  • 没有找到相关文章

最新更新