Home > Net >  C # small problem about lock?
C # small problem about lock?

Time:12-01

A very simple piece of code!
Function of note:
1. Click a button to
2. Open two Windows

Problem of
Theory, after playing a pop-up window, I shut down automatically after 3 seconds to play the next;
Why I played 2 click the moment, why lock didn't work!

Please say the principle!

 
///& lt; summary>
///lock
///& lt;/summary>
Private static readonly object locker=new object ();
///& lt; summary>
///button event
///& lt;/summary>
///& lt; Param name="sender" & gt;
///& lt; Param name="e" & gt;
Private void btn_test_Click (object sender, RoutedEventArgs e)
{
int n=0;
While (n++ & lt; 2)
{
Task. Factory. StartNew (()=& gt;
{
Application. The Current. The Dispatcher. The BeginInvoke (DispatcherPriority Background, new Action (()=& gt;
{
The lock (locker)
{
MessageBox. Show (" test ");
Thread.sleep (3000);
}
}));
});
}
}

CodePudding user response:

I know the answer,,,
  •  Tags:  
  • C#
  • Related