WPF UI refinement process background papers of non-UI update process

Original: WPF UI refinement process background papers of non-UI update process

 


  
  
  1. <Grid>
  2. <Grid.RowDefinitions>
  3. <RowDefinition Height= "11*"/>
  4. <RowDefinition Height= "29*"/>
  5. </Grid.RowDefinitions>
  6. <StackPanel Orientation= "Horizontal" Margin= "0" VerticalAlignment= "Center">
  7. <Label>开始数据</Label>
  8. <TextBox x:Name= "beginText" HorizontalAlignment= "Left" Height= "31" TextWrapping= "Wrap" Text= "100" VerticalAlignment= "Top" Width= "100"/>
  9. <Label>结束数据</Label>
  10. <TextBox x:Name= "endText" HorizontalAlignment= "Left" Height= "31" TextWrapping= "Wrap" Text= "1000000000" VerticalAlignment= "Top" Width= "100"/>
  11. <Button x:Name= "button" Content= "开始" HorizontalAlignment= "Center" VerticalAlignment= "Center" Width= "75" Click= "Button_Click"/>
  12. </StackPanel>
  13. <StackPanel Margin= "0" Grid.Row= "1">
  14. <TextBlock x:Name= "odd" TextWrapping= "Wrap" Text= "奇数数量:"/>
  15. <TextBlock x:Name= "even" TextWrapping= "Wrap" Text= "偶数数量:"/>
  16. </StackPanel>
  17. </Grid>

 


  
  
  1. private int oddcount = 0;
  2. private int evencount = 0;
  3. public void Make(int from ,int to)
  4. {
  5. for ( int i = from; i < to; i++)
  6. {
  7. if (i % 2 == 0)
  8. {
  9. evencount++;
  10. }
  11. else
  12. {
  13. oddcount++;
  14. }
  15. }
  16. }
  17. private void Button_Click(object sender, RoutedEventArgs e)
  18. {
  19. int from= 0;
  20. int to = 0;
  21. if( int.TryParse(beginText.Text, out from)&& int.TryParse(endText.Text, out to) )
  22. {
  23. button.IsEnabled = false;
  24. ThreadPool.QueueUserWorkItem(_ =>
  25. {
  26. Make( from, to);
  27. Dispatcher.BeginInvoke( new Action(() =>
  28. {
  29. odd.Text = "奇数数量:" + oddcount;
  30. even.Text = "偶数数量:" + evencount;
  31. button.IsEnabled = true;
  32. }));
  33. });
  34. }
  35. }

 


  
  
  1. Dispatcher.BeginInvoke( new Action(() =>
  2. {
  3. //UI线程
  4. }));

 

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/12075536.html