Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing MessageBox on MouseDown eats up MouseUp event

Tags:

c#

mouseevent

wpf

I am working on a WPF application where i handled a mouse down event which eventually shows up MessageBox.. But after MessageBox appears on mouseDown, it eats up corresponding MouseUp event of a control.

Scenario can be easily reproduced by simply handling MouseDown and MouseUP event in WPF window as:-

private void Window_MouseDown(object sender, MouseButtonEventArgs e)
{
   MessageBox.show("Hello, Mouse down");
}

private void Window_MouseUP(object sender, MouseButtonEventArgs e)
{
   MessageBox.show("Hello, Mouse Up");
}

MouseUp message is never shown, once messagebox appears on MouseDown event.

like image 248
Karan Avatar asked Dec 02 '12 15:12

Karan


1 Answers

What about initializing a new instance of System.Threading.Thread to call the MessageBox so that the main user interface thread would not be interrupted by the prompt?

Example

private void Window_MouseDown(object sender, MouseEventArgs e)
{
    Thread mythread = new Thread(() => MessageBox.Show("Hello, Mouse Down")); //Initialize a new Thread to show our MessageBox within 
    mythread.Start(); //Start the thread
}

private void Window_MouseUP(object sender, MouseEventArgs e)
{
    Thread mythread = new Thread(() => MessageBox.Show("Hello, Mouse Up")); //Initialize a new Thread to show our MessageBox within 
    mythread.Start(); //Start the thread
}

Screenshot

Calling a MessageBox in a different thread to avoid interrupting the main user interface thread

Thanks,
I hope you find this helpful :)

like image 119
Picrofo Software Avatar answered Oct 12 '22 16:10

Picrofo Software