Note: we hope to further understand the working principles of the process by looking at these cases.
1. method 1 describes how to calculate prime numbers in the same window without affecting canvas operations.
Method 1
1 # Region Long-running calculation in Ui thread 2 3 Public Delegate Void Nextprimedelegate (); 4 Private Long Num = 3 ; 5 Private Bool Continuecalculating = False ; 6 Private Bool Fnotgateme = False ; 7 8 Private Void Btnprimenumber_click ( Object Sender, routedeventargs E) 9 { 10 If (Continuecalculating) 11 { 12 Continuecalculating = False ; 13 Btnprimenumber. content = " Resume " ; 14 } 15 Else 16 { 17 Continuecalculating = True ; 18 Btnprimenumber. content = " Stop " ; 19 20 // Obtain the system. Windows. Threading. dispatcherobject associated with system. Windows. Threading. Dispatcher 21 22 // Public dispatcheroperation begininvoke (dispatcherpriority priority, delegate method ); 23 // The specified delegate is asynchronously executed on the Thread associated with system. Windows. Threading. Dispatcher Based on the specified priority. 24 Btnprimenumber. Dispatcher. begininvoke (dispatcherpriority. Normal, New Nextprimedelegate (checknextnumber )); 25 26 } 27 } 28 29 Public Void Checknextnumber () 30 { 31 // Reset flag. 32 Fnotgateme = False ; 33 34 For ( Long I = 3 ; I <= math. SQRT (Num); I ++ ) 35 { 36 If (Num % I = 0 ) 37 { 38 // Set not-a-prime flag to true. 39 Fnotgateme = True ; 40 Break ; 41 } 42 } 43 44 // If a prime number. 45 If (! Fnotaprime) 46 { 47 Tbprime. Text = Num. tostring (); 48 } 49 50 Num + = 2 ; 51 52 If (Continuecalculating) 53 { 54 // 3. In the checknextnumber function, because the first parameter 55 // Passed into begininvoke is dispatcherpriority. systemidle (process the operation when the system is idle .), 56 // All of the checknextnumber workitem will not break the UI operation. 57 58 Btnprimenumber. Dispatcher. begininvoke ( 59 System. Windows. Threading. dispatcherpriority. systemidle, 60 New Nextprimedelegate ( This . Checknextnumber )); 61 } 62 } 63 64 # Endregion
Method 2
1 # Region Blocking operation in worker thread 2 3 Private Delegate Void Noargdelegate (); 4 Private Delegate Void Oneargdelegate (int32 [] Arg ); 5 6 // 1. When the retrieve data from server button is clicked, the click handle retrievedata function is called. 7 Private Void Btnretrievedata_click ( Object Sender, routedeventargs E) 8 { 9 This . Btnretrievedata. isenabled = False ; 10 This . Btnretrievedata. content = " Contacting Server " ; 11 12 Noargdelegate fetcher = New Noargdelegate ( This . Retrievedatafromserver ); 13 14 // 2. Then our codes use delegate. begininvoke to start a thread from the thread pool. 15 // This thread is used to perform the long operation of retrieving data. 16 Fetcher. begininvoke ( Null , Null ); 17 } 18 19 /// <Summary> 20 /// Retrieve data in a worker thread (Auxiliary thread ). 21 /// </Summary> 22 Private Void Retrievedatafromserver () 23 { 24 // 3. We use thread. Sleep (5000) to simulate a 5 seconds delay here. 25 // Simulate the delay from network access. 26 Thread. Sleep ( 5000 ); 27 28 // 4. The codes generate 4 random numbers as data and update them to the UI by calling the dispatcher. begininvoke (). 29 30 // Generate random data to be displayed. 31 Random Rand = New Random (); 32 Int32 [] DATA = { 33 Rand. Next ( 1000 ), Rand. Next ( 1000 ), 34 Rand. Next (1000 ), Rand. Next ( 1000 ) 35 }; 36 37 // Schedule the UPDATE function in the UI thread. 38 This . Dispatcher. begininvoke (system. Windows. Threading. dispatcherpriority. Normal, 39 New Oneargdelegate (updateuserinterface), data ); 40 41 } 42 43 /// <Summary> 44 /// Update the UI about the new data. The function runs in the UI thread. 45 /// </Summary> 46 /// <Param name = "data"> </param> 47 Private Void Updateuserinterface (int32 [] data) 48 { 49 This . Btnretrievedata. isenabled = True ; 50 This . Btnretrievedata. content = " Retrieve data from server " ; 51 This . Tbdata1.text = data [ 0 ]. Tostring (); 52 This . Tbdata2.text = data [ 1 ]. Tostring (); 53 This . Tbdata3.text = data [ 2 ]. Tostring (); 54 This . Tbdata4.text = data [ 3 ]. Tostring (); 55 } 56 57 # Endregion