BackgroundWorker
生活随笔
收集整理的這篇文章主要介紹了
BackgroundWorker
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Threading;namespace 異步編程
{class Program{static void Main(string[] args){MyDownloadString ds = new MyDownloadString();ds.DoRun();Console.ReadKey();}}class MyDownloadString{Stopwatch sw = new Stopwatch();public void DoRun(){const int LargeNumber = 6000000;sw.Start();Task<int> t1 = CountCharacterAsync(1, "http://www.microsoft.com");// t1.Wait(); 等待這個任務(wù)執(zhí)行完成再去執(zhí)行其他Task<int> t2 = CountCharacterAsync(2, "http://www.illustratedcsharp.com");//t2.Wait();// Console.WriteLine("3"+t2.Result);CountToAlargeNumber(1, LargeNumber);CountToAlargeNumber(2, LargeNumber);CountToAlargeNumber(3, LargeNumber);CountToAlargeNumber(4, LargeNumber);Console.WriteLine("4");Console.WriteLine("chars in www.microsoft.com : {0}", t1.Result);Console.WriteLine(" chars in www.illustatecsharp.com : {0}", t2.Result);Console.WriteLine("time in total : " + sw.Elapsed.TotalMilliseconds);}//async 相當(dāng)于一個標(biāo)志,標(biāo)志這個是異步方法,await 指明需要異步執(zhí)行的地方 ,一個//異步方法可以包含任意多個await表達式,如果一個不包括編譯器會報警,//異步方法的三種返回類型//Task<T> ,方法返回值,使用Task.Result獲得這個返回值//Task ,如果調(diào)用方法不需要從異步方法中返回某個值,但需要檢查異步方法的狀態(tài),此時使用Task,這是即使異步方法return了某個值,此時也不會獲取到//Void ,只是調(diào)用異步方法,沒有任何交互// await 指定一個異步執(zhí)行的任務(wù)(TASK),創(chuàng)建一個Task的最簡單方法是 Task.run() ,它是在不同線程上運行方法//Task.Delay 不同于thread.sleep 不會阻塞線程,線程可以繼續(xù)處理其他工作private async Task<int> CountCharacterAsync(int id ,string site){WebClient wc = new WebClient();Console.WriteLine("starting call {0} : {1} ms", id, sw.Elapsed.TotalMilliseconds);string result = await wc.DownloadStringTaskAsync(new Uri(site));Func<int, int> nf = new Func<int, int>(get10);await Task.Run(() => get10(2)); // 使用lambada表達式逃避task類型的約束Console.WriteLine(" Call {0} completed : {1} ms",id, sw.Elapsed.TotalMilliseconds);await Task.Delay(5000);return result.Length;}private void CountToAlargeNumber(int id ,int value){for (long i = 0; i < value; i++) ;Console.WriteLine(" End counting {0} : {1} ms ",id,sw.Elapsed.TotalMilliseconds );}private int get10(int x){return 10+x;}}
}
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms;namespace BackgroundWorker {public partial class Form1 : Form{public Form1(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){if (!backgroundWorker1.IsBusy){backgroundWorker1.RunWorkerAsync();}}private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e){for (int i = 1; i < 11; i++){Thread.Sleep(2000);backgroundWorker1.ReportProgress(i * 10);}}private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e){progressBar1.Value = e.ProgressPercentage;}} }
轉(zhuǎn)載于:https://www.cnblogs.com/Jeely/p/11001939.html
總結(jié)
以上是生活随笔為你收集整理的BackgroundWorker的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: aop-xml-环绕增强
- 下一篇: 微信浏览器不支持下载文件或应用解决方案