无码视频在线观看,99人妻,国产午夜视频,久久久久国产一级毛片高清版新婚

  • 始創(chuàng)于2000年 股票代碼:831685
    咨詢熱線:0371-60135900 注冊(cè)有禮 登錄
    • 掛牌上市企業(yè)
    • 60秒人工響應(yīng)
    • 99.99%連通率
    • 7*24h人工
    • 故障100倍補(bǔ)償
    全部產(chǎn)品
    您的位置: 網(wǎng)站首頁(yè) > 幫助中心>文章內(nèi)容

    細(xì)說(shuō)多線程(六) —— 異步 SqlCommand

    發(fā)布時(shí)間:  2012/9/16 6:37:12

    目錄

    一、線程的定義

    二、線程的基礎(chǔ)知識(shí)

    三、以ThreadStart方式實(shí)現(xiàn)多線程

    四、CLR線程池的工作者線程

    五、CLR線程池的I/O線程

    六、異步 SqlCommand

    七、并行編程與PLINQ

    八、計(jì)時(shí)器與鎖

     

    六、異步 SqlCommand

    從ADO.NET 2.0開始,SqlCommand就新增了幾個(gè)異步方法執(zhí)行SQL命令。相對(duì)于同步執(zhí)行方式,它使主線程不需要等待數(shù)據(jù)庫(kù)的返回結(jié)果,在使用復(fù)雜性查詢或 批量插入時(shí)將有效提高主線程的效率。使用異步SqlCommand的時(shí)候,請(qǐng)注意把ConnectionString 的 Asynchronous Processing 設(shè)置為 true 。

    注意:SqlCommand異步操作的特別之處在于線程并不依賴于CLR線程池,而是由Windows內(nèi)部提供,這比使用異步委托更有效率。但如果需要使用回調(diào)函數(shù)的時(shí)候,回調(diào)函數(shù)的線程依然是來(lái)自于CLR線程池的工作者線程。

    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)異步操作,利用輪詢方式監(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線程池最大線程數(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操作,利用輪詢方式監(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)狀
    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í)行線程并不屬于CLR線程池。

     

    如果覺得使用輪詢方式過(guò)于麻煩,可以使用回調(diào)函數(shù),但要注意當(dāng)調(diào)用回調(diào)函數(shù)時(shí),線程是來(lái)自于CLR線程池的工作者線程。

         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線程池最大線程數(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)狀
    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) 電話 037160135991 服務(wù)器租用,托管歡迎咨詢。

    本文出自:億恩科技【mszdt.com】

    服務(wù)器租用/服務(wù)器托管中國(guó)五強(qiáng)!虛擬主機(jī)域名注冊(cè)頂級(jí)提供商!15年品質(zhì)保障!--億恩科技[ENKJ.COM]

  • 您可能在找
  • 億恩北京公司:
  • 經(jīng)營(yíng)性ICP/ISP證:京B2-20150015
  • 億恩鄭州公司:
  • 經(jīng)營(yíng)性ICP/ISP/IDC證:豫B1.B2-20060070
  • 億恩南昌公司:
  • 經(jīng)營(yíng)性ICP/ISP證:贛B2-20080012
  • 服務(wù)器/云主機(jī) 24小時(shí)售后服務(wù)電話:0371-60135900
  • 虛擬主機(jī)/智能建站 24小時(shí)售后服務(wù)電話:0371-60135900
  • 專注服務(wù)器托管17年
    掃掃關(guān)注-微信公眾號(hào)
    0371-60135900
    Copyright© 1999-2019 ENKJ All Rights Reserved 億恩科技 版權(quán)所有  地址:鄭州市高新區(qū)翠竹街1號(hào)總部企業(yè)基地億恩大廈  法律顧問:河南亞太人律師事務(wù)所郝建鋒、杜慧月律師   京公網(wǎng)安備41019702002023號(hào)
      1
     
     
     
     

    0371-60135900
    7*24小時(shí)客服服務(wù)熱線