??????C#?????У??????????Щ????????CPU????????????????? UI ????????????????????UI??????????????????????????????????????????????????????????????????????????????ɡ?????????????????????Щ??????????????????????????????????????
???????? .NET ???????????????? BackgroundWorker ???????????????????????????BackgroundWorker ????????????????????л??????Щ?????????????????????? demo ???????????????÷????????? demo?м???1??100?????????????????μ??? sleep 600????demo ??UI???

?????÷?????
?????????????????BackgroundWorker ??????????? DoWork?????????????????????????????????RunWorkerAsync???????????
????private BackgroundWorker _demoBGWorker = new BackgroundWorker();
????_demoBGWorker.DoWork += BGWorker_DoWork;
????_demoBGWorker.RunWorkerAsync();
????private void BGWorker_DoWork(object sender?? DoWorkEventArgs e)
????{
????//????????к????????
????int sum = 0;
????for (int i = 0; i <= 100; i++)
????{
????sum += i;
????}
????}
??????????е????????????????????????????
???????????????????????????????????????
????????????????????????????????????UI????????
???????????????????????е???????????
????????????????????????????δ????
???????????????????????????Щ????
?????????????????????
????????100д????????????п?????????????????????????????Χ????????????100??????????????????????????????????????RunWorkerAsync??????????????????????????????????? object ????????????????????????κ?????????????????
????//?????????ù???????
????this.progressBarSum.Maximum = 100;
????_demoBGWorker.RunWorkerAsync(100);
????//????????o?? BGWorker_DoWork ??????
????private void BGWorker_DoWork(object sender?? DoWorkEventArgs e)
????{
????//????????к????????
????int endNumber = 0;
????if(e.Argument != null)
????{
????endNumber = (int)e.Argument;
????}
????int sum = 0;
????for (int i = 0; i <= endNumber; i++)
????{
????sum += i;
????}
????}
????BGWorker_DoWork????????????????? e ??Argument????????????????????????????
??????????????UI
????????????????????????????????????????????????????????????????????м????????UI????????BackgroundWorker???????????????????????????????????м????????и?UI??????????????濴?????????????
????_demoBGWorker.WorkerReportsProgress = true;
????_demoBGWorker.ProgressChanged += BGWorker_ProgressChanged;
???????????WorkerReportsProgress ????????? true??????ProgressChanged ??????????????
????private void BGWorker_ProgressChanged(object sender?? ProgressChangedEventArgs e)
????{
????//???????????????
????this.progressBarSum.Value = e.ProgressPercentage;
????//????и?????????????????????? e.UserState ??????????????????
????//??????? object ????????????????????????κ??????
????//????????? sum ?????? e.UserState ?????????????????????
????string message = e.UserState.ToString();
????this.labelSum.Text = message;
????}
???????????? BGWorker_DoWork??????
????private void BGWorker_DoWork(object sender?? DoWorkEventArgs e)
????{
????BackgroundWorker bgWorker = sender as BackgroundWorker;
????int endNumber = 0;
????if(e.Argument != null)
????{
????endNumber = (int)e.Argument;
????}
????int sum = 0;
????for (int i = 0; i <= endNumber; i++)
????{
????sum += i;
????string message = "Current sum is: " + sum.ToString();
????//ReportProgress ?????????????? ProcessChanged ????????????
????//?????????????? int???????н????
????//????и?????????????????????? ReportProgress ????????????
????//???????????????????????????????
????bgWorker.ReportProgress(i?? message);
????Thread.Sleep(600);
????}
????}
????OK??????????????????????????????????????