資源簡(jiǎn)介
C#異步操作 異步查詢數(shù)據(jù)庫(kù) 異步處理一行一行加載數(shù)據(jù)
代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.Linq;
using?System.Text;
using?System.ComponentModel;
using?System.Data;
using?System.Data.SqlClient;
using?System.Windows.Forms;
using?System.Threading;
namespace?WindowsFormsApplication1
{
????public??class?AsynchronousQuery
????{
????????private?System.ComponentModel.BackgroundWorker?backgroundWorker;
????????public?AsynchronousQuery()
????????{
???????????
????????}
????????///?
????????///?開始線程
????????///?
????????public?void?InitAsynchronous(string?strSql)
????????{
????????????backgroundWorker?=?new?BackgroundWorker();
????????????//指定提供進(jìn)度通知??
????????????backgroundWorker.WorkerReportsProgress?=?true;
????????????//提供中斷功能??
????????????backgroundWorker.WorkerSupportsCancellation?=?true;
????????????//線程的主要功能是處理事件??
????????????//開啟線程執(zhí)行工作??,C#進(jìn)度條實(shí)現(xiàn)之異步實(shí)例
????????????backgroundWorker.DoWork?+=?new?DoWorkEventHandler(BackgroundWorker_DoWork);
????????????//指定使用的功能來(lái)處理進(jìn)度?
????????????backgroundWorker.ProgressChanged?+=?new?ProgressChangedEventHandler(BackgroundWorker_ProgressChanged);
????????????//進(jìn)度條結(jié)束完成工作?
????????????//1.工作完成??
????????????//2.工作錯(cuò)誤異常??
????????????//3.取消工作?
????????????backgroundWorker.RunWorkerCompleted?+=?new?RunWorkerCompletedEventHandler(BackgroundWorker_RunWorkerCompleted);
????????????//如果進(jìn)度條需要參數(shù)?
????????????//調(diào)用System.ComponentModel.BackgroundWorker.RunWorkerAsync??
????????????//傳入你的參數(shù)至System.ComponentModel.BackgroundWorker.DoWork??
????????????//提取參數(shù)?
????????????//System.ComponentModel.DoWorkEventArgs.Argument???
????????????//1.調(diào)用bgwInsertData的RunWorkerAsync方法,用來(lái)引發(fā)DoWork事件
????????????backgroundWorker.RunWorkerAsync(strSql);
????????}
????????///?
????????///?操作開始時(shí)在另一個(gè)線程上運(yùn)行的事件處理程序
????????///?
????????private?void?BackgroundWorker_DoWork(object?sender?DoWorkEventArgs?e)
????????{
????????????BackgroundWorker?worker?=?sender?as?BackgroundWorker;
????????????//2.在DoWork中調(diào)用自定義函數(shù),并將引發(fā)DoWork事件的sender傳遞出去
????????????DataTable?dt?=?Query(e.Argument?as?string).Tables[0];
????????????int?countt?=?dt.Rows.Count;
????????????if?(countt?>?0)
????????????{
???????????????
????????????????????if?(worker.CancellationPending)
????????????????????{
????????????????????????e.Cancel?=?true;
????????????????????????return;
????????????????????}
????????????????????else
????????????????????{
????????????????????????//傳遞給ProgressChanged中數(shù)據(jù)
????????????????????????
????????????????????????worker.ReportProgress(100?dt);
???????????????????????
????????????????????}
????????????????
????????????}
????????}
????????///?
????????///?當(dāng)輔助線程指示某些操作已經(jīng)運(yùn)行時(shí)引發(fā)
????????///?
????????private?void?BackgroundWorker_ProgressChanged(object?sender?ProgressChangedEventArgs?e)
????????{
????????????//?DoWork中?worker.ReportProgress((i?+?1)?*?(100?/?countt)?dt.Rows[i].ItemArray);?
????????????//?第一個(gè)參數(shù)是e.ProgressPercentage??進(jìn)度
??????????
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件???????5947??2011-07-01?15:49??異步操作\WindowsFormsApplication1\AsynchronousQuery.cs
?????文件???????3754??2011-07-01?15:49??異步操作\WindowsFormsApplication1\AsynchronousQuery1.cs
?????文件???????1209??2011-07-01?14:37??異步操作\WindowsFormsApplication1\AsynchronousQuery1.Designer.cs
?????文件????2469888??2007-06-02?16:15??異步操作\WindowsFormsApplication1\bin\Debug\DevComponents.DotNetBar2.dll
?????文件??????23040??2011-07-01?16:39??異步操作\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.exe
?????文件??????71168??2011-07-01?16:39??異步操作\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.pdb
?????文件??????14328??2011-07-01?17:09??異步操作\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.vshost.exe
?????文件????????490??2007-07-21?01:33??異步操作\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.vshost.exe.manifest
?????文件???????2253??2011-07-01?09:46??異步操作\WindowsFormsApplication1\Form1.cs
?????文件???????1983??2011-07-01?09:46??異步操作\WindowsFormsApplication1\Form1.Designer.cs
?????文件???????5814??2011-07-01?09:46??異步操作\WindowsFormsApplication1\Form1.resx
?????文件????????737??2011-07-01?11:00??異步操作\WindowsFormsApplication1\Form2.cs
?????文件???????2082??2011-07-01?11:00??異步操作\WindowsFormsApplication1\Form2.Designer.cs
?????文件???????6019??2011-07-01?11:00??異步操作\WindowsFormsApplication1\Form2.resx
?????文件???????7388??2011-07-01?15:54??異步操作\WindowsFormsApplication1\Form3.cs
?????文件???????4854??2011-07-01?14:21??異步操作\WindowsFormsApplication1\Form3.Designer.cs
?????文件???????5814??2011-07-01?14:19??異步操作\WindowsFormsApplication1\Form3.resx
?????文件???????1243??2011-07-01?16:40??異步操作\WindowsFormsApplication1\Form4.cs
?????文件???????4861??2011-07-01?15:57??異步操作\WindowsFormsApplication1\Form4.Designer.cs
?????文件???????5814??2011-07-01?15:57??異步操作\WindowsFormsApplication1\Form4.resx
?????文件???????5428??2011-07-01?13:28??異步操作\WindowsFormsApplication1\MainForm.cs
?????文件???????6088??2011-07-01?13:28??異步操作\WindowsFormsApplication1\MainForm.Designer.cs
?????文件???????5814??2011-07-01?13:28??異步操作\WindowsFormsApplication1\MainForm.resx
?????文件???????5702??2011-07-01?10:01??異步操作\WindowsFormsApplication1\obj\Debug\ResolveAssemblyReference.cache
?????文件????????180??2011-07-01?15:24??異步操作\WindowsFormsApplication1\obj\Debug\WindowsFormsApplication1.backgroundWorkerTest.resources
?????文件???????1921??2011-07-01?17:09??異步操作\WindowsFormsApplication1\obj\Debug\WindowsFormsApplication1.csproj.FileListAbsolute.txt
?????文件???????1089??2011-07-01?15:57??異步操作\WindowsFormsApplication1\obj\Debug\WindowsFormsApplication1.csproj.GenerateResource.Cache
?????文件??????23040??2011-07-01?16:39??異步操作\WindowsFormsApplication1\obj\Debug\WindowsFormsApplication1.exe
?????文件????????180??2011-07-01?15:24??異步操作\WindowsFormsApplication1\obj\Debug\WindowsFormsApplication1.Form2.resources
?????文件????????180??2011-07-01?15:57??異步操作\WindowsFormsApplication1\obj\Debug\WindowsFormsApplication1.Form4.resources
............此處省略29個(gè)文件信息
評(píng)論
共有 條評(píng)論