目錄
一、線(xiàn)程的定義
二、線(xiàn)程的基礎(chǔ)知識(shí)
三、以ThreadStart方式實(shí)現(xiàn)多線(xiàn)程
四、CLR線(xiàn)程池的工作者線(xiàn)程
五、CLR線(xiàn)程池的I/O線(xiàn)程
六、異步 SqlCommand
七、并行編程與PLINQ
八、計(jì)時(shí)器與鎖
六、異步 SqlCommand
從ADO.NET 2.0開(kāi)始,SqlCommand就新增了幾個(gè)異步方法執(zhí)行SQL命令。相對(duì)于同步執(zhí)行方式,它使主線(xiàn)程不需要等待數(shù)據(jù)庫(kù)的返回結(jié)果,在使用復(fù)雜性查詢(xún)或 批量插入時(shí)將有效提高主線(xiàn)程的效率。使用異步SqlCommand的時(shí)候,請(qǐng)注意把ConnectionString 的 Asynchronous Processing 設(shè)置為 true 。
注意:SqlCommand異步操作的特別之處在于線(xiàn)程并不依賴(lài)于CLR線(xiàn)程池,而是由Windows內(nèi)部提供,這比使用異步委托更有效率。但如果需要使用回調(diào)函數(shù)的時(shí)候,回調(diào)函數(shù)的線(xiàn)程依然是來(lái)自于CLR線(xiàn)程池的工作者線(xiàn)程。
SqlCommand有以下幾個(gè)方法支持異步操作:
public IAsyncResult BeginExecuteNonQuery (......)
public int EndExecuteNonQuery(IAsyncResult)
public IAsyncResult BeginExecuteReader(......)
public SqlDataReader EndExecuteReader(IAsyncResult)
public IAsyncResult BeginExecuteXmlReader (......)
public XmlReader EndExecuteXmlReader(IAsyncResult)
由于使用方式相似,此處就以 BeginExecuteNonQuery 為例子,介紹一下異步SqlCommand的使用。首先建立connectionString,注意把Asynchronous Processing設(shè)置為true來(lái)啟動(dòng)異步命令,然后把SqlCommand.CommandText設(shè)置為 WAITFOR DELAY "0:0:3" 來(lái)虛擬數(shù)據(jù)庫(kù)操作。再通過(guò)BeginExecuteNonQuery啟動(dòng)異步操作,利用輪詢(xún)方式監(jiān)測(cè)操作情況。最后在操作完成后使用 EndExecuteNonQuery完成異步操作。
1 class Program
2 {
3 //把Asynchronous Processing設(shè)置為true 4 static string connectionString = "Data Source=LESLIE-PC;Initial Catalog=Business;“+ 5 "Integrated Security=True;Asynchronous Processing=true";
6
7 static void Main(string[] args)
8 {
9 //把CLR線(xiàn)程池最大線(xiàn)程數(shù)設(shè)置為1000 10 ThreadPool.SetMaxThreads(1000, 1000);
11 ThreadPoolMessage("Start");
12
13 //使用WAITFOR DELAY命令來(lái)虛擬操作 14 SqlConnection connection = new SqlConnection(connectionString);
15 SqlCommand command = new SqlCommand("WAITFOR DELAY '0:0:3';", connection);
16 connection.Open();
17
18 //啟動(dòng)異步SqlCommand操作,利用輪詢(xún)方式監(jiān)測(cè)操作 19 IAsyncResult result = command.BeginExecuteNonQuery();
20 ThreadPoolMessage("BeginRead");
21 while (!result.AsyncWaitHandle.WaitOne(500))
22 Console.WriteLine("Main thread do work........");
23
24 //結(jié)束異步SqlCommand 25 int count= command.EndExecuteNonQuery(result);
26 ThreadPoolMessage("\nCompleted");
27 Console.ReadKey();
28 }
29
30 //顯示線(xiàn)程池現(xiàn)狀 31 static void ThreadPoolMessage(string data)
32 {
33 int a, b;
34 ThreadPool.GetAvailableThreads(out a, out b);
35 string message = string.Format("{0}\n CurrentThreadId is {1}\n "+
36 "WorkerThreads is:{2} CompletionPortThreads is :{3}\n",
37 data, Thread.CurrentThread.ManagedThreadId, a.ToString(), b.ToString());
38 Console.WriteLine(message);
39 }
40 }
注意運(yùn)行結(jié)果,SqlCommand的異步執(zhí)行線(xiàn)程并不屬于CLR線(xiàn)程池。
![](http://img1.51cto.com/attachment/201204/160127907.jpg)
如果覺(jué)得使用輪詢(xún)方式過(guò)于麻煩,可以使用回調(diào)函數(shù),但要注意當(dāng)調(diào)用回調(diào)函數(shù)時(shí),線(xiàn)程是來(lái)自于CLR線(xiàn)程池的工作者線(xiàn)程。
class Program
{
//把Asynchronous Processing設(shè)置為true static string connectionString = "Data Source=LESLIE-PC;Initial Catalog=Business;”+
“Integrated Security=True;Asynchronous Processing=true";
static void Main(string[] args)
{
//把CLR線(xiàn)程池最大線(xiàn)程數(shù)設(shè)置為1000 ThreadPool.SetMaxThreads(1000, 1000);
ThreadPoolMessage("Start");
//使用WAITFOR DELAY命令來(lái)虛擬操作 SqlConnection connection = new SqlConnection(connectionString);
SqlCommand command = new SqlCommand("WAITFOR DELAY '0:0:3';", connection);
connection.Open();
//啟動(dòng)異步SqlCommand操作,并把SqlCommand對(duì)象傳遞到回調(diào)函數(shù) IAsyncResult result = command.BeginExecuteNonQuery(
new AsyncCallback(AsyncCallbackMethod),command);
Console.ReadKey();
}
static void AsyncCallbackMethod(IAsyncResult result)
{
Thread.Sleep(200);
ThreadPoolMessage("AsyncCallback");
SqlCommand command = (SqlCommand)result.AsyncState;
int count=command.EndExecuteNonQuery(result);
command.Connection.Close();
}
//顯示線(xiàn)程池現(xiàn)狀 static void ThreadPoolMessage(string data)
{
int a, b;
ThreadPool.GetAvailableThreads(out a, out b);
string message = string.Format("{0}\n CurrentThreadId is {1}\n "+
"WorkerThreads is:{2} CompletionPortThreads is :{3}\n",
data, Thread.CurrentThread.ManagedThreadId, a.ToString(), b.ToString());
Console.WriteLine(message);
}
}
億恩-天使(QQ:530997) 電話(huà) 037160135991 服務(wù)器租用,托管歡迎咨詢(xún)。
本文出自:億恩科技【www.allwellnessguide.com】
服務(wù)器租用/服務(wù)器托管中國(guó)五強(qiáng)!虛擬主機(jī)域名注冊(cè)頂級(jí)提供商!15年品質(zhì)保障!--億恩科技[ENKJ.COM]
|