六种多线程方法解决UI线程堵塞

{"root":{"nodeName":"document","nodeValue":null,"nodeType":"0","display":"block","nodeAttrs":[],"childNodes":[{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"color: rgb(51, 51, 51);;"}],"childNodes":[{"nodeName":"text","nodeValue":"一、六种多线程方法","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"color: rgb(51, 51, 51);;"}],"childNodes":[{"nodeName":"text","nodeValue":".NET Framework2.0框架提供了至少4种方式实现多线程,它们是“BackgroundWorker”组件、委托的异步调用、线程池ThreadPool以及线程类Thread;.NET Framework 4.0增加了任务并行库TPL和PLINQ技术,可利用Task和并行计算的方法实现。下面列举这6种方法。","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"color: rgb(51, 51, 51);;"}],"childNodes":[{"nodeName":"text","nodeValue":"1.      BackgroundWorker组件","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"color: rgb(51, 51, 51);;"}],"childNodes":[{"nodeName":"text","nodeValue":"命名空间:System.ComponentModel","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"color: rgb(51, 51, 51);;"}],"childNodes":[{"nodeName":"text","nodeValue":"程序集:System.dll","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"color: rgb(51, 51, 51);;"}],"childNodes":[{"nodeName":"text","nodeValue":"BackgroundWorker可以用于协助开发WinForm应用程序或WPF应用程序。它作为一个组件发布,提供工作线程的进度反馈、完成事件和取消工作线程方法。在Visual Studio设计器界面里,通过“工具箱”方便地将它加入设计界面,还能通过“属性”窗口设置它的属性和事件。","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"color: rgb(51, 51, 51);;"}],"childNodes":[{"nodeName":"text","nodeValue":"将工作代码写在DoWork事件处理程序里,在主线程(UI线程)调用BackgroundWorker对象的RunWorkerAsync方法即可在一个独立的线程里启动DoWork事件的处理程序。","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"div","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"background-color: rgb(231, 229, 220); overflow: auto; color: rgb(51, 51, 51); line-height: 26px;;"}],"childNodes":[{"nodeName":"div","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[],"childNodes":[{"nodeName":"div","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"line-height: normal; color: rgb(192, 192, 192); background-color: rgb(248, 248, 248); border-left: 3px solid rgb(108, 226, 108);;"}],"childNodes":[{"nodeName":"strong","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[],"childNodes":[{"nodeName":"text","nodeValue":"[csharp]","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"text","nodeValue":" ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]}]},{"nodeName":"ol","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"border: none; background-color: rgba(255, 255, 255, 1); color: rgba(92, 92, 92, 1);"}],"childNodes":[{"nodeName":"li","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"border-style: none none none solid; border-left-color: rgba(108, 226, 108, 1); list-style: outside; color: inherit; line-height: 18px;"}],"childNodes":[{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[],"childNodes":[{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 0, 0, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"backgroundWorker1.RunWorkerAsync(","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 102, 153, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"new","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":" Parameters(PrimesFrom,PrimesTo));  ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]}]}]}]}]}]},{"nodeName":"br","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[],"childNodes":[]},{"nodeName":"br","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[],"childNodes":[]},{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"color: rgb(51, 51, 51);;"}],"childNodes":[{"nodeName":"text","nodeValue":"若需要在UI上显示工作进度,先使BackgroundWorker对象的WorkerReportsProgress属性设置为True,然后在DoWork事件处理程序里直接用BackgroundWorker对象的ReportProgress方法向UI线程报告工作进度和进度信息,编写BackgroundWorker对象的ProgressChanged事件处理程序,来获得工作进度,更新UI上的进度条等。","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"color: rgb(51, 51, 51);;"}],"childNodes":[{"nodeName":"text","nodeValue":"通常我们还希望点击“取消”按钮,能取消后台工作任务。先使BackgroundWorker对象的WorkerSupportsCancellation属性设置为True,然后在“取消”按钮的单击事件处理程序里直接调用BackgroundWorker对象的CancelAsync方法;在DoWork事件处理程序里,通过BackgroundWorker对象的CancellationPending属性便可得知是否有请求取消操作。","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"div","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"background-color: rgb(231, 229, 220); overflow: auto; color: rgb(51, 51, 51); line-height: 26px;;"}],"childNodes":[{"nodeName":"div","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[],"childNodes":[{"nodeName":"div","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"line-height: normal; color: rgb(192, 192, 192); background-color: rgb(248, 248, 248); border-left: 3px solid rgb(108, 226, 108);;"}],"childNodes":[{"nodeName":"strong","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[],"childNodes":[{"nodeName":"text","nodeValue":"[csharp]","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"text","nodeValue":" ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]}]},{"nodeName":"ol","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"border: none; background-color: rgba(255, 255, 255, 1); color: rgba(92, 92, 92, 1);"}],"childNodes":[{"nodeName":"li","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"border-style: none none none solid; border-left-color: rgba(108, 226, 108, 1); list-style: outside; color: inherit; line-height: 18px;"}],"childNodes":[{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[],"childNodes":[{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 0, 0, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"backgroundWorker1.CancelAsync();  ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]}]}]}]}]}]},{"nodeName":"br","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[],"childNodes":[]},{"nodeName":"br","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[],"childNodes":[]},{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"color: rgb(51, 51, 51);;"}],"childNodes":[{"nodeName":"text","nodeValue":"当DoWork事件处理程序返回后,会在UI线程上产生RunWorkerCompleted事件。","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"color: rgb(51, 51, 51);;"}],"childNodes":[{"nodeName":"text","nodeValue":"2.      委托的异步调用","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"color: rgb(51, 51, 51);;"}],"childNodes":[{"nodeName":"text","nodeValue":".NET Framework中的许多对象支持同步和异步两种调用方法,它们的异步调用方法名称如BeginXXX。委托也支持同步调用(Invoke)和异步调用(BeginInvoke)两种方式。异步调用是不阻塞当前线程,使委托的方法与调用方代码异步执行;也可以在后台线程里,通过调用支持异步方法的.NET Framework对象(如WinForm的Form对象和WPF的Dispatcher对象)委托的代码,使代码在这些对象所在的线程里执行——这个技巧在后台线程请求执行UI线程上的代码时非常有用。","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"div","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"background-color: rgb(231, 229, 220); overflow: auto; color: rgb(51, 51, 51); line-height: 26px;;"}],"childNodes":[{"nodeName":"div","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[],"childNodes":[{"nodeName":"div","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"line-height: normal; color: rgb(192, 192, 192); background-color: rgb(248, 248, 248); border-left: 3px solid rgb(108, 226, 108);;"}],"childNodes":[{"nodeName":"strong","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[],"childNodes":[{"nodeName":"text","nodeValue":"[csharp]","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"text","nodeValue":" ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]}]},{"nodeName":"ol","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"border: none; background-color: rgba(255, 255, 255, 1); color: rgba(92, 92, 92, 1);"}],"childNodes":[{"nodeName":"li","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"border-style: none none none solid; border-left-color: rgba(108, 226, 108, 1); list-style: outside; color: inherit; line-height: 18px;"}],"childNodes":[{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[],"childNodes":[{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 0, 0, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 102, 153, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"if","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":" (","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 102, 153, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"this","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":".InvokeRequired) ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 130, 0, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"//Form1的多线程方法中的代码片段(WPF中也有类似的属性)","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"  ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]}]}]}]},{"nodeName":"li","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"border-style: none none none solid; border-left-color: rgba(108, 226, 108, 1); list-style: outside; background-color: rgba(248, 248, 248, 1); line-height: 18px;"}],"childNodes":[{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[],"childNodes":[{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 0, 0, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"{  ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]}]}]},{"nodeName":"li","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"border-style: none none none solid; border-left-color: rgba(108, 226, 108, 1); list-style: outside; color: inherit; line-height: 18px;"}],"childNodes":[{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[],"childNodes":[{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 0, 0, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"varupdate = ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 102, 153, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"new","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":" Action(TaskCompleted); ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 130, 0, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"//调用TaskCompleted方法更新UI","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"  ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]}]}]}]},{"nodeName":"li","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"border-style: none none none solid; border-left-color: rgba(108, 226, 108, 1); list-style: outside; background-color: rgba(248, 248, 248, 1); line-height: 18px;"}],"childNodes":[{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[],"childNodes":[{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 0, 0, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 102, 153, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"this","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":".BeginInvoke(update);  ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]}]}]}]},{"nodeName":"li","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"border-style: none none none solid; border-left-color: rgba(108, 226, 108, 1); list-style: outside; color: inherit; line-height: 18px;"}],"childNodes":[{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[],"childNodes":[{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 0, 0, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"}  ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]}]}]}]}]},{"nodeName":"br","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[],"childNodes":[]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"color: rgb(51, 51, 51); line-height: 26px;;"}],"childNodes":[{"nodeName":"text","nodeValue":" ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"color: rgb(51, 51, 51);;"}],"childNodes":[{"nodeName":"text","nodeValue":"在当前的类或者一个新类里编写一个后台执行代码的入口方法,然后在UI线程里声明指向此入口方法的委托对象,执行委托对象的BeginInvoke方法即可。委托对象的BeginInvoke方法的参数由两部分组成,第一部分是委托函数的参数,第二部分是委托方法异步调用完成后启动的方法和参数,可以不指定第二部分。","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"div","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"background-color: rgb(231, 229, 220); overflow: auto; color: rgb(51, 51, 51); line-height: 26px;;"}],"childNodes":[{"nodeName":"div","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[],"childNodes":[{"nodeName":"div","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"line-height: normal; color: rgb(192, 192, 192); background-color: rgb(248, 248, 248); border-left: 3px solid rgb(108, 226, 108);;"}],"childNodes":[{"nodeName":"strong","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[],"childNodes":[{"nodeName":"text","nodeValue":"[csharp]","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"text","nodeValue":" ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]}]},{"nodeName":"ol","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"border: none; background-color: rgba(255, 255, 255, 1); color: rgba(92, 92, 92, 1);"}],"childNodes":[{"nodeName":"li","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"border-style: none none none solid; border-left-color: rgba(108, 226, 108, 1); list-style: outside; color: inherit; line-height: 18px;"}],"childNodes":[{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[],"childNodes":[{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 0, 0, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"varworker = ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 102, 153, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"new","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":" Action","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]},{"nodeName":"parameters","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[],"childNodes":[{"nodeName":"text","nodeValue":"(FindPrimesViaDelegate); ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]}]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 130, 0, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"//委托","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"  ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]}]}]}]},{"nodeName":"li","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"border-style: none none none solid; border-left-color: rgba(108, 226, 108, 1); list-style: outside; background-color: rgba(248, 248, 248, 1); line-height: 18px;"}],"childNodes":[{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[],"childNodes":[{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 0, 0, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"worker.BeginInvoke(","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 102, 153, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"new","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":" Parameters(PrimesFrom,PrimesTo), TaskComplete, ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 102, 153, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"null","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":");  ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]}]}]}]}]}]},{"nodeName":"br","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[],"childNodes":[]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"color: rgb(51, 51, 51); line-height: 26px;;"}],"childNodes":[{"nodeName":"text","nodeValue":" ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"color: rgb(51, 51, 51);;"}],"childNodes":[{"nodeName":"text","nodeValue":"从.NET Framework 3.5开始,支持9个传入参数的Action泛型委托和8个传入参数、1个返回值的Func泛型委托,到.NETFramework 4.0,支持传入参数达16个的Action和Func泛型委托。它们被定义在System命名空间,程序集mscorlib.dll,从.NET Framework 3.5时代后,较少的使用Delegate关键字自定义委托了。","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"color: rgb(51, 51, 51);;"}],"childNodes":[{"nodeName":"text","nodeValue":"3.      线程池ThreadPool","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"color: rgb(51, 51, 51);;"}],"childNodes":[{"nodeName":"text","nodeValue":"命名空间:System.Threading","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"color: rgb(51, 51, 51);;"}],"childNodes":[{"nodeName":"text","nodeValue":"程序集:mscorlib.dll","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"color: rgb(51, 51, 51);;"}],"childNodes":[{"nodeName":"text","nodeValue":"每个进程拥有一个线程池。托管代码的线程池的最多支持线程数目与.NET Framework版本及CPU数目等硬件环境有关。在.NET Framework 4.0中,默认每个可用的CPU处理器增加250个辅助线程和1000个I/O线程。可使用SetMaxThreads方法更改线程池的最多线程数(注:承载.NET Framework的非托管代码,如C++,可使用mscoree.h头文件的CorSetMaxThreads函数更改线程池大小)。除了SetMaxThreads方法,还可以使用GetMaxThreads、GetMinThreads、SetMinThreads方法获得或更改线程数。.NET Framework中的许多多线程的类或组件(如System.Threading.Timer),就是在线程池中运行的。","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"color: rgb(51, 51, 51);;"}],"childNodes":[{"nodeName":"text","nodeValue":"需要记住一点的是,线程池线程都是后台线程,即线程池线程的IsBackground属性都为True,全部前台线程退出后,线程池线程将被强行中断。","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"color: rgb(51, 51, 51);;"}],"childNodes":[{"nodeName":"text","nodeValue":"用QueueUserWorkItem方法将一个无参数或者仅一个参数的void方法加入到线程池启动。","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"div","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"background-color: rgb(231, 229, 220); overflow: auto; color: rgb(51, 51, 51); line-height: 26px;;"}],"childNodes":[{"nodeName":"div","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[],"childNodes":[{"nodeName":"div","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"line-height: normal; color: rgb(192, 192, 192); background-color: rgb(248, 248, 248); border-left: 3px solid rgb(108, 226, 108);;"}],"childNodes":[{"nodeName":"strong","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[],"childNodes":[{"nodeName":"text","nodeValue":"[csharp]","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"text","nodeValue":" ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]}]},{"nodeName":"ol","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"border: none; background-color: rgba(255, 255, 255, 1); color: rgba(92, 92, 92, 1);"}],"childNodes":[{"nodeName":"li","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"border-style: none none none solid; border-left-color: rgba(108, 226, 108, 1); list-style: outside; color: inherit; line-height: 18px;"}],"childNodes":[{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[],"childNodes":[{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 0, 0, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"ThreadPool.QueueUserWorkItem(FindPrimes, newParameters(PrimesFrom, PrimesTo)); ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 130, 0, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"//启动一个线程池线程","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"  ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]}]}]}]}]}]},{"nodeName":"br","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[],"childNodes":[]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"color: rgb(51, 51, 51); line-height: 26px;;"}],"childNodes":[{"nodeName":"text","nodeValue":" ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"color: rgb(51, 51, 51);;"}],"childNodes":[{"nodeName":"text","nodeValue":"4.      线程Thread","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"color: rgb(51, 51, 51);;"}],"childNodes":[{"nodeName":"text","nodeValue":"命名空间:System.Threading","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"color: rgb(51, 51, 51);;"}],"childNodes":[{"nodeName":"text","nodeValue":"程序集:mscorlib.dll","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"color: rgb(51, 51, 51);;"}],"childNodes":[{"nodeName":"text","nodeValue":"将一个无参数或者仅一个参数的void方法委托给Thread实例,调用Thread对象的Start方法启动一个线程,可对它进行优先级、前后台线程、线程单元状态、线程状态及名称等更多细致的控制。","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"div","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"background-color: rgb(231, 229, 220); overflow: auto; color: rgb(51, 51, 51); line-height: 26px;;"}],"childNodes":[{"nodeName":"div","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[],"childNodes":[{"nodeName":"div","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"line-height: normal; color: rgb(192, 192, 192); background-color: rgb(248, 248, 248); border-left: 3px solid rgb(108, 226, 108);;"}],"childNodes":[{"nodeName":"strong","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[],"childNodes":[{"nodeName":"text","nodeValue":"[csharp]","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"text","nodeValue":" ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]}]},{"nodeName":"ol","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"border: none; background-color: rgba(255, 255, 255, 1); color: rgba(92, 92, 92, 1);"}],"childNodes":[{"nodeName":"li","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"border-style: none none none solid; border-left-color: rgba(108, 226, 108, 1); list-style: outside; color: inherit; line-height: 18px;"}],"childNodes":[{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[],"childNodes":[{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 0, 0, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"var t= ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 102, 153, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"new","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":" Thread(FindPrimes)  ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]}]}]}]},{"nodeName":"li","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"border-style: none none none solid; border-left-color: rgba(108, 226, 108, 1); list-style: outside; background-color: rgba(248, 248, 248, 1); line-height: 18px;"}],"childNodes":[{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[],"childNodes":[{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 0, 0, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"         {  ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]}]}]},{"nodeName":"li","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"border-style: none none none solid; border-left-color: rgba(108, 226, 108, 1); list-style: outside; color: inherit; line-height: 18px;"}],"childNodes":[{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[],"childNodes":[{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 0, 0, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"             Name = ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 0, 255, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"\"FindPrimes\"","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":",  ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]}]}]}]},{"nodeName":"li","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"border-style: none none none solid; border-left-color: rgba(108, 226, 108, 1); list-style: outside; background-color: rgba(248, 248, 248, 1); line-height: 18px;"}],"childNodes":[{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[],"childNodes":[{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 0, 0, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"             IsBackground = ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 102, 153, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"true","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"  ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]}]}]}]},{"nodeName":"li","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"border-style: none none none solid; border-left-color: rgba(108, 226, 108, 1); list-style: outside; color: inherit; line-height: 18px;"}],"childNodes":[{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[],"childNodes":[{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 0, 0, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"         };  ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]}]}]},{"nodeName":"li","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"border-style: none none none solid; border-left-color: rgba(108, 226, 108, 1); list-style: outside; background-color: rgba(248, 248, 248, 1); line-height: 18px;"}],"childNodes":[{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[],"childNodes":[{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 0, 0, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"t.Start(","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 102, 153, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"new","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":" Parameters(PrimesFrom, PrimesTo)); ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 130, 0, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"//启动一个线程","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"  ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]}]}]}]}]}]},{"nodeName":"br","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[],"childNodes":[]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"color: rgb(51, 51, 51); line-height: 26px;;"}],"childNodes":[{"nodeName":"text","nodeValue":" ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"color: rgb(51, 51, 51);;"}],"childNodes":[{"nodeName":"text","nodeValue":"5.      任务Task","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"color: rgb(51, 51, 51);;"}],"childNodes":[{"nodeName":"text","nodeValue":"命名空间:System.Threading.Tasks","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"color: rgb(51, 51, 51);;"}],"childNodes":[{"nodeName":"text","nodeValue":"程序集:mscorlib.dll","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"color: rgb(51, 51, 51);;"}],"childNodes":[{"nodeName":"text","nodeValue":"Task作为.NETFramework 4.0推崇的多线程代替办法,方便的控制任务的有序或并行执行,充分地发挥多核CPU性能,将多项任务平衡分配给每个可用的CPU。由于任务中的某些方法使用了数据共享锁技术,可使用Dispose方法显式地销毁这些资源。泛型版本的任务还能取得其返回结果,通常任务作为数组并发执行的。利用Windows任务管理器或性能监视器能监视程序的CPU利用率的波形图。","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"div","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"background-color: rgb(231, 229, 220); overflow: auto; color: rgb(51, 51, 51); line-height: 26px;;"}],"childNodes":[{"nodeName":"div","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[],"childNodes":[{"nodeName":"div","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"line-height: normal; color: rgb(192, 192, 192); background-color: rgb(248, 248, 248); border-left: 3px solid rgb(108, 226, 108);;"}],"childNodes":[{"nodeName":"strong","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[],"childNodes":[{"nodeName":"text","nodeValue":"[csharp]","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"text","nodeValue":" ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]}]},{"nodeName":"ol","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"border: none; background-color: rgba(255, 255, 255, 1); color: rgba(92, 92, 92, 1);"}],"childNodes":[{"nodeName":"li","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"border-style: none none none solid; border-left-color: rgba(108, 226, 108, 1); list-style: outside; color: inherit; line-height: 18px;"}],"childNodes":[{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[],"childNodes":[{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 0, 0, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"_tokenSource= ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 102, 153, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"new","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":" CancellationTokenSource();","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 130, 0, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"//用于取消任务","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"  ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]}]}]}]},{"nodeName":"li","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"border-style: none none none solid; border-left-color: rgba(108, 226, 108, 1); list-style: outside; background-color: rgba(248, 248, 248, 1); line-height: 18px;"}],"childNodes":[{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[],"childNodes":[{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 0, 0, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"Task.Factory.StartNew(FindPrimesInTask,","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 102, 153, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"new","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":" Parameters(PrimesFrom,PrimesTo), _tokenSource.Token); ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 130, 0, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"//启动一个任务","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"  ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]}]}]}]}]}]},{"nodeName":"br","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[],"childNodes":[]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"color: rgb(51, 51, 51); line-height: 26px;;"}],"childNodes":[{"nodeName":"text","nodeValue":" ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"color: rgb(51, 51, 51);;"}],"childNodes":[{"nodeName":"text","nodeValue":"6.      并行计算Parallel","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"color: rgb(51, 51, 51);;"}],"childNodes":[{"nodeName":"text","nodeValue":"命名空间:System.Threading.Tasks","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"color: rgb(51, 51, 51);;"}],"childNodes":[{"nodeName":"text","nodeValue":"程序集:mscorlib.dll","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"color: rgb(51, 51, 51);;"}],"childNodes":[]},{"nodeName":"text","nodeValue":"充分发挥CPU的多核性能,Parallel.Invoke方法可以同时运行多个并行任务,Parallel.For和Parallel.ForEach方法可以并行循环和迭代IEnumerable","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]},{"nodeName":"t","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[],"childNodes":[{"nodeName":"text","nodeValue":"集合。","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]},{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[],"childNodes":[]},{"nodeName":"div","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"background-color: rgb(231, 229, 220); overflow: auto; color: rgb(51, 51, 51); line-height: 26px;;"}],"childNodes":[{"nodeName":"div","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[],"childNodes":[{"nodeName":"div","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"line-height: normal; color: rgb(192, 192, 192); background-color: rgb(248, 248, 248); border-left: 3px solid rgb(108, 226, 108);;"}],"childNodes":[{"nodeName":"strong","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[],"childNodes":[{"nodeName":"text","nodeValue":"[csharp]","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"text","nodeValue":" ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]}]},{"nodeName":"ol","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"border: none; background-color: rgba(255, 255, 255, 1); color: rgba(92, 92, 92, 1);"}],"childNodes":[{"nodeName":"li","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"border-style: none none none solid; border-left-color: rgba(108, 226, 108, 1); list-style: outside; color: inherit; line-height: 18px;"}],"childNodes":[{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[],"childNodes":[{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 0, 0, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"Parallel.For(parameters.Min, parameters.Max,  ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]}]}]}]},{"nodeName":"li","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"border-style: none none none solid; border-left-color: rgba(108, 226, 108, 1); list-style: outside; background-color: rgba(248, 248, 248, 1); line-height: 18px;"}],"childNodes":[{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[],"childNodes":[{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 0, 0, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"    (count, loop) =>  ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]}]}]},{"nodeName":"li","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"border-style: none none none solid; border-left-color: rgba(108, 226, 108, 1); list-style: outside; color: inherit; line-height: 18px;"}],"childNodes":[{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[],"childNodes":[{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 0, 0, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"    {  ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]}]}]},{"nodeName":"li","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"border-style: none none none solid; border-left-color: rgba(108, 226, 108, 1); list-style: outside; background-color: rgba(248, 248, 248, 1); line-height: 18px;"}],"childNodes":[{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[],"childNodes":[{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 0, 0, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 130, 0, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"//并行执行的代码","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"  ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]}]}]}]},{"nodeName":"li","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"border-style: none none none solid; border-left-color: rgba(108, 226, 108, 1); list-style: outside; color: inherit; line-height: 18px;"}],"childNodes":[{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[],"childNodes":[{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 0, 0, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 130, 0, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"//注:在Parallel.For里,第一个参数是min至max的自变量,","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"  ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]}]}]}]},{"nodeName":"li","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"border-style: none none none solid; border-left-color: rgba(108, 226, 108, 1); list-style: outside; background-color: rgba(248, 248, 248, 1); line-height: 18px;"}],"childNodes":[{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[],"childNodes":[{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 0, 0, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 130, 0, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"//第二个参数是指这个并行循环体参数,可执行中断并行循环体等控制方法","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"  ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]}]}]}]},{"nodeName":"li","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"border-style: none none none solid; border-left-color: rgba(108, 226, 108, 1); list-style: outside; color: inherit; line-height: 18px;"}],"childNodes":[{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[],"childNodes":[{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 0, 0, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"}  ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]}]}]}]}]},{"nodeName":"br","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[],"childNodes":[]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"color: rgb(51, 51, 51); line-height: 26px;;"}],"childNodes":[{"nodeName":"text","nodeValue":" ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"color: rgb(51, 51, 51);;"}],"childNodes":[{"nodeName":"text","nodeValue":"二、取消线程","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"color: rgb(51, 51, 51);;"}],"childNodes":[{"nodeName":"text","nodeValue":"除了BackgroundWorker组件,在.NETFramework 4.0之前的多线程框架中是不提供类似CancellationToken类型用于支持多线程的取消请求的。常用的办法是轮询检查一个线程间共享的取消标记的方式,获得取消请求,甚至编写一个线程管理器来增强多线程的可控性。","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"color: rgb(51, 51, 51);;"}],"childNodes":[{"nodeName":"text","nodeValue":".NET Framework4.0对多线程和并行运算进行了增强和改进,","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]},{"nodeName":"strong","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[],"childNodes":[{"nodeName":"text","nodeValue":"CancellationTokenSource","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"text","nodeValue":"对象可以在多线程方案中更有效的发出取消请求。","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"color: rgb(51, 51, 51);;"}],"childNodes":[{"nodeName":"text","nodeValue":"命名空间:System.Threading","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"color: rgb(51, 51, 51);;"}],"childNodes":[{"nodeName":"text","nodeValue":"程序集:mscorlib.dll","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"div","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"background-color: rgb(231, 229, 220); overflow: auto; color: rgb(51, 51, 51); line-height: 26px;;"}],"childNodes":[{"nodeName":"div","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[],"childNodes":[{"nodeName":"div","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"line-height: normal; color: rgb(192, 192, 192); background-color: rgb(248, 248, 248); border-left: 3px solid rgb(108, 226, 108);;"}],"childNodes":[{"nodeName":"strong","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[],"childNodes":[{"nodeName":"text","nodeValue":"[csharp]","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"text","nodeValue":" ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]}]},{"nodeName":"ol","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"border: none; background-color: rgba(255, 255, 255, 1); color: rgba(92, 92, 92, 1);"}],"childNodes":[{"nodeName":"li","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"border-style: none none none solid; border-left-color: rgba(108, 226, 108, 1); list-style: outside; color: inherit; line-height: 18px;"}],"childNodes":[{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[],"childNodes":[{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 0, 0, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 130, 0, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"//1.声明一个CancellationTokenSource对象","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"  ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]}]}]}]},{"nodeName":"li","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"border-style: none none none solid; border-left-color: rgba(108, 226, 108, 1); list-style: outside; background-color: rgba(248, 248, 248, 1); line-height: 18px;"}],"childNodes":[{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[],"childNodes":[{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 0, 0, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 102, 153, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"private","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":" CancellationTokenSource _tokenSource;  ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]}]}]}]},{"nodeName":"li","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"border-style: none none none solid; border-left-color: rgba(108, 226, 108, 1); list-style: outside; color: inherit; line-height: 18px;"}],"childNodes":[{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[],"childNodes":[{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 0, 0, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 130, 0, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"//2.实例化_tokenSource对象","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"  ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]}]}]}]},{"nodeName":"li","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"border-style: none none none solid; border-left-color: rgba(108, 226, 108, 1); list-style: outside; background-color: rgba(248, 248, 248, 1); line-height: 18px;"}],"childNodes":[{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[],"childNodes":[{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 0, 0, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"_tokenSource= ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 102, 153, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"new","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":" CancellationTokenSource();  ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]}]}]}]},{"nodeName":"li","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"border-style: none none none solid; border-left-color: rgba(108, 226, 108, 1); list-style: outside; color: inherit; line-height: 18px;"}],"childNodes":[]},{"nodeName":"li","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"border-style: none none none solid; border-left-color: rgba(108, 226, 108, 1); list-style: outside; background-color: rgba(248, 248, 248, 1); line-height: 18px;"}],"childNodes":[{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[],"childNodes":[{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 0, 0, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 130, 0, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"//3.在支持CancellationToken的代码里判断取消请求","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"  ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]}]}]}]},{"nodeName":"li","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"border-style: none none none solid; border-left-color: rgba(108, 226, 108, 1); list-style: outside; color: inherit; line-height: 18px;"}],"childNodes":[{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[],"childNodes":[{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 0, 0, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 102, 153, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"if","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"(_tokenSource.IsCancellationRequested)  ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]}]}]}]},{"nodeName":"li","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"border-style: none none none solid; border-left-color: rgba(108, 226, 108, 1); list-style: outside; background-color: rgba(248, 248, 248, 1); line-height: 18px;"}],"childNodes":[{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[],"childNodes":[{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 0, 0, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"{  ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]}]}]},{"nodeName":"li","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"border-style: none none none solid; border-left-color: rgba(108, 226, 108, 1); list-style: outside; color: inherit; line-height: 18px;"}],"childNodes":[{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[],"childNodes":[{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 0, 0, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"    loop.Stop();  ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]}]}]},{"nodeName":"li","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"border-style: none none none solid; border-left-color: rgba(108, 226, 108, 1); list-style: outside; background-color: rgba(248, 248, 248, 1); line-height: 18px;"}],"childNodes":[{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[],"childNodes":[{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 0, 0, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"}  ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]}]}]},{"nodeName":"li","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"border-style: none none none solid; border-left-color: rgba(108, 226, 108, 1); list-style: outside; color: inherit; line-height: 18px;"}],"childNodes":[{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[],"childNodes":[{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 0, 0, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 130, 0, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"//4.在控制线程代码里用调用取消请求","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"  ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]}]}]}]},{"nodeName":"li","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"border-style: none none none solid; border-left-color: rgba(108, 226, 108, 1); list-style: outside; background-color: rgba(248, 248, 248, 1); line-height: 18px;"}],"childNodes":[{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[],"childNodes":[{"nodeName":"span","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":"border: none; color: rgba(0, 0, 0, 1); background-color: inherit;"}],"childNodes":[{"nodeName":"text","nodeValue":"_tokenSource.Cancel();  ","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]}]}]}]}]},{"nodeName":"br","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[],"childNodes":[]},{"nodeName":"br","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[],"childNodes":[]},{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"color: rgb(51, 51, 51);;"}],"childNodes":[{"nodeName":"text","nodeValue":"三、演示","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"color: rgb(51, 51, 51);;"}],"childNodes":[{"nodeName":"text","nodeValue":"在Wrox出版社的《VisualBasic 2005高级编程》第22章有一个求10000内素数的例子讲解.NET的多线程技术,我写了类似的演示代码,演示6种多线程的方法在后台计算素数,而不阻塞UI。","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"color: rgb(51, 51, 51);;"}],"childNodes":[{"nodeName":"img","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[{"attrName":"style","attrValue":" border-style: none;;"},{"attrName":"src","attrValue":"http://hi.csdn.net/attachment/201110/7/0_1318027299Y0JK.gif"}],"childNodes":[]},{"nodeName":"br","nodeValue":null,"nodeType":"1","display":"inline","nodeAttrs":[],"childNodes":[]}]},{"nodeName":"p","nodeValue":null,"nodeType":"1","display":"block","nodeAttrs":[{"attrName":"style","attrValue":"color: rgb(51, 51, 51);;"}],"childNodes":[{"nodeName":"text","nodeValue":"值得一提的是,在任务并发计算素数的演示里,利用CPU多核计算,得到结果所花费的时间有显著的提高。上图中最后一个波峰可看到CPU 0和CPU 1的并行工作情况。","nodeType":"3","display":"inline","nodeAttrs":[],"childNodes":[]}]}]}]},"imageUrls":["https://hi.csdn.net/attachment/201110/7/0_1318027299Y0JK.gif"],"text":"一、六种多线程方法.NET Framework2.0框架提供了至少4种方式实现多线程,它们是“BackgroundWorker”组件、委托的异步调用、线程池ThreadPool以及线程类Thread;.NET Framework 4.0增加了任务并行库TPL和PLINQ技术,可利用Task和并行计算的方法实现。下面列举这6种方法。1.      BackgroundWorker组件命名空间:System.ComponentModel程序集:System.dllBackgroundWorker可以用于协助开发WinForm应用程序或WPF应用程序。它作为一个组件发布,提供工作线程的进度反馈、完成事件和取消工作线程方法。在Visual Studio设计器界面里,通过“工具箱”方便地将它加入设计界面,还能通过“属性”窗口设置它的属性和事件。将工作代码写在DoWork事件处理程序里,在主线程(UI线程)调用BackgroundWorker对象的RunWorkerAsync方法即可在一个独立的线程里启动DoWork事件的处理程序。[csharp] backgroundWorker1.RunWorkerAsync(new Parameters(PrimesFrom,PrimesTo));  若需要在UI上显示工作进度,先使BackgroundWorker对象的WorkerReportsProgress属性设置为True,然后在DoWork事件处理程序里直接用BackgroundWorker对象的ReportProgress方法向UI线程报告工作进度和进度信息,编写BackgroundWorker对象的ProgressChanged事件处理程序,来获得工作进度,更新UI上的进度条等。通常我们还希望点击“取消”按钮,能取消后台工作任务。先使BackgroundWorker对象的WorkerSupportsCancellation属性设置为True,然后在“取消”按钮的单击事件处理程序里直接调用BackgroundWorker对象的CancelAsync方法;在DoWork事件处理程序里,通过BackgroundWorker对象的CancellationPending属性便可得知是否有请求取消操作。[csharp] backgroundWorker1.CancelAsync();  当DoWork事件处理程序返回后,会在UI线程上产生RunWorkerCompleted事件。2.      委托的异步调用.NET Framework中的许多对象支持同步和异步两种调用方法,它们的异步调用方法名称如BeginXXX。委托也支持同步调用(Invoke)和异步调用(BeginInvoke)两种方式。异步调用是不阻塞当前线程,使委托的方法与调用方代码异步执行;也可以在后台线程里,通过调用支持异步方法的.NET Framework对象(如WinForm的Form对象和WPF的Dispatcher对象)委托的代码,使代码在这些对象所在的线程里执行——这个技巧在后台线程请求执行UI线程上的代码时非常有用。[csharp] if (this.InvokeRequired) //Form1的多线程方法中的代码片段(WPF中也有类似的属性)  {  varupdate = new Action(TaskCompleted); //调用TaskCompleted方法更新UI  this.BeginInvoke(update);  }   在当前的类或者一个新类里编写一个后台执行代码的入口方法,然后在UI线程里声明指向此入口方法的委托对象,执行委托对象的BeginInvoke方法即可。委托对象的BeginInvoke方法的参数由两部分组成,第一部分是委托函数的参数,第二部分是委托方法异步调用完成后启动的方法和参数,可以不指定第二部分。[csharp] varworker = new Action(FindPrimesViaDelegate); //委托  worker.BeginInvoke(new Parameters(PrimesFrom,PrimesTo), TaskComplete, null);   从.NET Framework 3.5开始,支持9个传入参数的Action泛型委托和8个传入参数、1个返回值的Func泛型委托,到.NETFramework 4.0,支持传入参数达16个的Action和Func泛型委托。它们被定义在System命名空间,程序集mscorlib.dll,从.NET Framework 3.5时代后,较少的使用Delegate关键字自定义委托了。3.      线程池ThreadPool命名空间:System.Threading程序集:mscorlib.dll每个进程拥有一个线程池。托管代码的线程池的最多支持线程数目与.NET Framework版本及CPU数目等硬件环境有关。在.NET Framework 4.0中,默认每个可用的CPU处理器增加250个辅助线程和1000个I/O线程。可使用SetMaxThreads方法更改线程池的最多线程数(注:承载.NET Framework的非托管代码,如C++,可使用mscoree.h头文件的CorSetMaxThreads函数更改线程池大小)。除了SetMaxThreads方法,还可以使用GetMaxThreads、GetMinThreads、SetMinThreads方法获得或更改线程数。.NET\n Framework中的许多多线程的类或组件(如System.Threading.Timer),就是在线程池中运行的。需要记住一点的是,线程池线程都是后台线程,即线程池线程的IsBackground属性都为True,全部前台线程退出后,线程池线程将被强行中断。用QueueUserWorkItem方法将一个无参数或者仅一个参数的void方法加入到线程池启动。[csharp] ThreadPool.QueueUserWorkItem(FindPrimes, newParameters(PrimesFrom, PrimesTo)); //启动一个线程池线程   4.      线程Thread命名空间:System.Threading程序集:mscorlib.dll将一个无参数或者仅一个参数的void方法委托给Thread实例,调用Thread对象的Start方法启动一个线程,可对它进行优先级、前后台线程、线程单元状态、线程状态及名称等更多细致的控制。[csharp] var t= new Thread(FindPrimes)           {               Name = \"FindPrimes\",               IsBackground = true           };  t.Start(new Parameters(PrimesFrom, PrimesTo)); //启动一个线程   5.      任务Task命名空间:System.Threading.Tasks程序集:mscorlib.dllTask作为.NETFramework 4.0推崇的多线程代替办法,方便的控制任务的有序或并行执行,充分地发挥多核CPU性能,将多项任务平衡分配给每个可用的CPU。由于任务中的某些方法使用了数据共享锁技术,可使用Dispose方法显式地销毁这些资源。泛型版本的任务还能取得其返回结果,通常任务作为数组并发执行的。利用Windows任务管理器或性能监视器能监视程序的CPU利用率的波形图。[csharp] _tokenSource= new CancellationTokenSource();//用于取消任务  Task.Factory.StartNew(FindPrimesInTask,new Parameters(PrimesFrom,PrimesTo), _tokenSource.Token); //启动一个任务   6.      并行计算Parallel命名空间:System.Threading.Tasks程序集:mscorlib.dll充分发挥CPU的多核性能,Parallel.Invoke方法可以同时运行多个并行任务,Parallel.For和Parallel.ForEach方法可以并行循环和迭代IEnumerable集合。[csharp] Parallel.For(parameters.Min, parameters.Max,      (count, loop) =>      {  //并行执行的代码  //注:在Parallel.For里,第一个参数是min至max的自变量,  //第二个参数是指这个并行循环体参数,可执行中断并行循环体等控制方法  }   二、取消线程除了BackgroundWorker组件,在.NETFramework 4.0之前的多线程框架中是不提供类似CancellationToken类型用于支持多线程的取消请求的。常用的办法是轮询检查一个线程间共享的取消标记的方式,获得取消请求,甚至编写一个线程管理器来增强多线程的可控性。.NET Framework4.0对多线程和并行运算进行了增强和改进,CancellationTokenSource对象可以在多线程方案中更有效的发出取消请求。命名空间:System.Threading程序集:mscorlib.dll[csharp] //1.声明一个CancellationTokenSource对象  private CancellationTokenSource _tokenSource;  //2.实例化_tokenSource对象  _tokenSource= new CancellationTokenSource();  //3.在支持CancellationToken的代码里判断取消请求  if(_tokenSource.IsCancellationRequested)  {      loop.Stop();  }  //4.在控制线程代码里用调用取消请求  _tokenSource.Cancel();  三、演示在Wrox出版社的《VisualBasic 2005高级编程》第22章有一个求10000内素数的例子讲解.NET的多线程技术,我写了类似的演示代码,演示6种多线程的方法在后台计算素数,而不阻塞UI。值得一提的是,在任务并发计算素数的演示里,利用CPU多核计算,得到结果所花费的时间有显著的提高。上图中最后一个波峰可看到CPU 0和CPU 1的并行工作情况。"}

(0)

相关推荐