WPF 精修篇 BackgroundWorker
标签:imp title label cal keyword ndt 登录 ble run
原文:WPF 精修篇 BackgroundWorker
效果
-
-
-
-
-
-
-
"Horizontal" Margin="0,28" VerticalAlignment="Center">
-
-
"beginText" HorizontalAlignment="Left" Height="31" TextWrapping="Wrap" Text="100" VerticalAlignment="Top" Width="100"/>
-
-
"endText" HorizontalAlignment="Left" Height="31" TextWrapping="Wrap" Text="1000000000" VerticalAlignment="Top" Width="100"/>
-
-
-
-
"0" Grid.Row="1" Grid.RowSpan="2">
-
"odd" TextWrapping="Wrap" Text="奇数数量:"/>
-
"even" TextWrapping="Wrap" Text="偶数数量:"/>
-
-
-
-
-
private int evencount =0;
-
public void Make(int from ,int to)
-
-
-
-
int num = (to - from) / 100;
-
for (int i = from; i
-
-
if (worker.CancellationPending == true)
-
-
-
-
-
-
-
-
-
-
-
-
-
if (i % num == 0 && (worker != null) && (worker.WorkerReportsProgress = true))
-
-
worker.ReportProgress(i / num);
-
-
-
-
-
BackgroundWorker worker = null;
-
private void Button_Click(object sender, RoutedEventArgs e)
-
-
-
-
if(int.TryParse(beginText.Text,out from)&&int.TryParse(endText.Text,out to) )
-
-
button.IsEnabled = false;
-
-
-
-
worker = new BackgroundWorker();
-
-
worker.WorkerSupportsCancellation = true;
-
-
worker.WorkerReportsProgress = true;
-
-
worker.ProgressChanged+=worker_ProgressChanged;
-
-
-
worker.DoWork+=worker_DoWork;
-
-
worker.RunWorkerCompleted += worker_RunWorkerCompleted;
-
-
worker.RunWorkerAsync(new Tupleint, int>(from, to));
-
-
-
-
-
-
private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
-
-
Prebar.Value = e.ProgressPercentage;
-
-
-
void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
-
-
// throw new NotImplementedException();
-
if (sender is BackgroundWorker)
-
-
-
-
-
-
-
-
-
-
odd.Text = "奇数数量:" + oddcount;
-
even.Text = "偶数数量:" + evencount;
-
-
BackgroundWorker k = (BackgroundWorker)sender;
-
k.DoWork -= worker_DoWork;
-
k.RunWorkerCompleted -= worker_RunWorkerCompleted;
-
-
-
-
-
-
-
private void worker_DoWork(object sender, DoWorkEventArgs e)
-
-
var args = ( Tupleint, int>)e.Argument;
-
Make(args.Item1, args.Item2);
-
if (worker.CancellationPending == true)
-
-
-
-
-
-
-
private void Cancel_Click(object sender, RoutedEventArgs e)
-
-
-
-
WPF 精修篇 BackgroundWorker
标签:imp title label cal keyword ndt 登录 ble run
原文地址:https://www.cnblogs.com/lonelyxmas/p/12075538.html
评论