WPF 精修篇 非UI进程后台更新UI进程
Posted lonelyxmas
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF 精修篇 非UI进程后台更新UI进程相关的知识,希望对你有一定的参考价值。
原文:WPF 精修篇 非UI进程后台更新UI进程
- <Grid>
- <Grid.RowDefinitions>
- <RowDefinition Height="11*"/>
- <RowDefinition Height="29*"/>
- </Grid.RowDefinitions>
- <StackPanel Orientation="Horizontal" Margin="0" VerticalAlignment="Center">
- <Label>开始数据</Label>
- <TextBox x:Name="beginText" HorizontalAlignment="Left" Height="31" TextWrapping="Wrap" Text="100" VerticalAlignment="Top" Width="100"/>
- <Label>结束数据</Label>
- <TextBox x:Name="endText" HorizontalAlignment="Left" Height="31" TextWrapping="Wrap" Text="1000000000" VerticalAlignment="Top" Width="100"/>
- <Button x:Name="button" Content="开始" HorizontalAlignment="Center" VerticalAlignment="Center" Width="75" Click="Button_Click"/>
- </StackPanel>
- <StackPanel Margin="0" Grid.Row="1">
- <TextBlock x:Name="odd" TextWrapping="Wrap" Text="奇数数量:"/>
- <TextBlock x:Name="even" TextWrapping="Wrap" Text="偶数数量:"/>
- </StackPanel>
- </Grid>
- private int oddcount =0;
- private int evencount =0;
- public void Make(int from ,int to)
- {
- for (int i = from; i < to; i++)
- {
- if (i % 2 == 0)
- {
- evencount++;
- }
- else
- {
- oddcount++;
- }
- }
- }
- private void Button_Click(object sender, RoutedEventArgs e)
- {
- int from=0;
- int to = 0;
- if(int.TryParse(beginText.Text,out from)&&int.TryParse(endText.Text,out to) )
- {
- button.IsEnabled = false;
- ThreadPool.QueueUserWorkItem(_ =>
- {
- Make(from, to);
- Dispatcher.BeginInvoke(new Action(() =>
- {
- odd.Text = "奇数数量:" + oddcount;
- even.Text = "偶数数量:" + evencount;
- button.IsEnabled = true;
- }));
-
- });
-
- }
-
- }
- Dispatcher.BeginInvoke(new Action(() =>
- {
- //UI线程
- }));
以上是关于WPF 精修篇 非UI进程后台更新UI进程的主要内容,如果未能解决你的问题,请参考以下文章