C# BackgroundWorker ???
???????????? ???????[ 2016/9/28 10:16:06 ] ??????????????????? C# BackgroundWorker
??????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??????????????????????????????????????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11