wpf-Prompt window (thread)
Start blogging again small start ~
Thread Prompt window self-feeling can also, but read a lot of Daniel said line Cheng use, as if the thread release problem, but if on a thread, should not be a problem, if there is a thread in-depth understanding of Daniel can explain one or two ~
Start today's essay ~
First, create a new form Remindwinview, simply add a textblock,text= "hint Windows 1th", the main form of the CS file defined thread, and initialize the thread, the code is as follows
1 #regionThread2 3 //Thread State4 Private BOOLFlag;5 //Defining Threads6 PrivateThread Tremind;7 //Events8 Private Delegate voidThreadgo ();9 //whether to log in timely reminder (true, initial reminder, false, initial 10 seconds after the reminder);Ten Private BOOLIsremindintime =true; One A #endregion - - PublicMainWindow () the { - InitializeComponent (); - - //Start Thread +Flag =true; - //Initializing Threads +Tremind =NewThread (NewThreadStart (ThreadProc)); A //Thread Run Level atTremind.priority =threadpriority.highest; - //set cell state before thread starts - tremind.setapartmentstate (apartmentstate.sta); - //Running Threads - Tremind.start (); -}
Defining Thread Events
1 /// <summary>2 ///Thread Events3 /// </summary>4 Private voidThreadProc ()5 {6 while(flag)7 {8 if(isremindintime)9 {TenSystem.Windows.Application.Current.Dispatcher.Invoke (Dispatcherpriority.background,NewThreadgo (Threadtogo)); One //immediate reminder, every 10 seconds reminder A intMilseconds = Convert.ToInt32 (Ten) * +; - Thread.Sleep (milseconds); - } the Else - { - //10 seconds to start reminding every 10 seconds - intMilseconds = Convert.ToInt32 (Ten) * +; + Thread.Sleep (milseconds); -System.Windows.Application.Current.Dispatcher.Invoke (Dispatcherpriority.background,NewThreadgo (Threadtogo)); + } A } at}
Execution events
1 /// <summary>2 ///Execution Events3 /// </summary>4 Private voidThreadtogo ()5 {6 if(! Isexistshowview ("Remindwinview_name"))7 {8Remindwinview W =NewRemindwinview ();9W.left = System.Windows.SystemParameters.PrimaryScreenWidth-W.width;TenW.top = System.Windows.SystemParameters.WorkArea.Height-W.height; One w.show (); A } -}
An event that is released to a thread after the main form is closed
1 /// <summary>2 ///main form Close event3 /// </summary>4 /// <param name= "Sender" ></param>5 /// <param name= "E" ></param>6 Private voidWindow_closed (Objectsender, EventArgs e)7 {8 //The thread stops after the main form is closed, or the thread is running9 Tremind.abort ();Ten}
There is also a judgment to determine whether the cue window exists
1 /// <summary>2 ///Check if the form exists3 /// </summary>4 /// <param name= "Winname" ></param>5 /// <returns></returns>6 Private BOOLIsexistshowview (stringwinname)7 {8 BOOLIsexist =false;9 Ten foreach(Window wininchApplication.Current.Windows) One { A if(Win. Name = =winname) - { -Isexist =true; the win. Activate (); - Break; - } - } + returnisexist; -}
I remember WPF also has a hint window, temporarily forget, if you want to, add it ~
wpf-Prompt window (thread)