WPF中的多进程(Threading)处理实例(一)
2021-04-13 05:28
标签:操作 lag logs not random sim tap summary tar 说明:希望通过揣摩这些案例,能进一步了解进程的工作原理。 1.方法一描述的是在同一窗口中,在计算素数的同时而不影响Canvas的工作。 WPF中的多进程(Threading)处理实例(一) 标签:操作 lag logs not random sim tap summary tar 原文地址:https://www.cnblogs.com/lonelyxmas/p/8986722.html 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 fNotAPrime = 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 //获取与此 System.Windows.Threading.DispatcherObject 关联的 System.Windows.Threading.Dispatcher
21
22 //public DispatcherOperation BeginInvoke(DispatcherPriority priority, Delegate method);
23 //按指定的优先级在与 System.Windows.Threading.Dispatcher 关联的线程上异步执行指定的委托。
24 btnPrimeNumber.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new NextPrimeDelegate(CheckNextNumber));
25
26 }
27 }
28
29 public void CheckNextNumber()
30 {
31 // Reset flag.
32 fNotAPrime = false;
33
34 for (long i = 3; i )
35 {
36 if (num % i == 0)
37 {
38 // Set not-a-prime flag to true.
39 fNotAPrime = 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(在系统空闲时处理操作。),
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
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 ///
文章标题:WPF中的多进程(Threading)处理实例(一)
文章链接:http://soscw.com/index.php/essay/75058.html