1. <var id="fe6gj"></var>

    <rp id="fe6gj"><nav id="fe6gj"></nav></rp>

    <noframes id="fe6gj"><cite id="fe6gj"></cite>

    <ins id="fe6gj"><button id="fe6gj"><p id="fe6gj"></p></button></ins>
    1. <tt id="fe6gj"><i id="fe6gj"><sub id="fe6gj"></sub></i></tt>
        始創于2000年 股票代碼:831685
        咨詢熱線:0371-60135900 注冊有禮 登錄
        • 掛牌上市企業
        • 60秒人工響應
        • 99.99%連通率
        • 7*24h人工
        • 故障100倍補償
        您的位置: 網站首頁 > 幫助中心>文章內容

        細說多線程(六) —— 異步 SqlCommand

        發布時間:  2012/9/16 6:37:12

        目錄

        一、線程的定義

        二、線程的基礎知識

        三、以ThreadStart方式實現多線程

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

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

        六、異步 SqlCommand

        七、并行編程與PLINQ

        八、計時器與鎖

         

        六、異步 SqlCommand

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

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

        SqlCommand有以下幾個方法支持異步操作:

        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設置為true來啟動異步命令,然后把SqlCommand.CommandText設置為 WAITFOR DELAY "0:0:3" 來虛擬數據庫操作。再通過BeginExecuteNonQuery啟動異步操作,利用輪詢方式監測操作情況。最后在操作完成后使用 EndExecuteNonQuery完成異步操作。

         1     class Program
         2     {
         3         //把Asynchronous Processing設置為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線程池最大線程數設置為1000
        10 ThreadPool.SetMaxThreads(1000, 1000); 11 ThreadPoolMessage("Start"); 12 13 //使用WAITFOR DELAY命令來虛擬操作
        14 SqlConnection connection = new SqlConnection(connectionString); 15 SqlCommand command = new SqlCommand("WAITFOR DELAY '0:0:3';", connection); 16 connection.Open(); 17 18 //啟動異步SqlCommand操作,利用輪詢方式監測操作
        19 IAsyncResult result = command.BeginExecuteNonQuery(); 20 ThreadPoolMessage("BeginRead"); 21 while (!result.AsyncWaitHandle.WaitOne(500)) 22 Console.WriteLine("Main thread do work........"); 23 24 //結束異步SqlCommand
        25 int count= command.EndExecuteNonQuery(result); 26 ThreadPoolMessage("\nCompleted"); 27 Console.ReadKey(); 28 } 29 30 //顯示線程池現狀
        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 }

        注意運行結果,SqlCommand的異步執行線程并不屬于CLR線程池。

         

        如果覺得使用輪詢方式過于麻煩,可以使用回調函數,但要注意當調用回調函數時,線程是來自于CLR線程池的工作者線程。

             class Program
             {
                 //把Asynchronous Processing設置為true
        static string connectionString = "Data Source=LESLIE-PC;Initial Catalog=Business;”+ “Integrated Security=True;Asynchronous Processing=true"; static void Main(string[] args) { //把CLR線程池最大線程數設置為1000
        ThreadPool.SetMaxThreads(1000, 1000); ThreadPoolMessage("Start"); //使用WAITFOR DELAY命令來虛擬操作
        SqlConnection connection = new SqlConnection(connectionString); SqlCommand command = new SqlCommand("WAITFOR DELAY '0:0:3';", connection); connection.Open(); //啟動異步SqlCommand操作,并把SqlCommand對象傳遞到回調函數
        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(); } //顯示線程池現狀
        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 服務器租用,托管歡迎咨詢。

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

        服務器租用/服務器托管中國五強!虛擬主機域名注冊頂級提供商!15年品質保障!--億恩科技[ENKJ.COM]

      1. 您可能在找
      2. 億恩北京公司:
      3. 經營性ICP/ISP證:京B2-20150015
      4. 億恩鄭州公司:
      5. 經營性ICP/ISP/IDC證:豫B1.B2-20060070
      6. 億恩南昌公司:
      7. 經營性ICP/ISP證:贛B2-20080012
      8. 服務器/云主機 24小時售后服務電話:0371-60135900
      9. 虛擬主機/智能建站 24小時售后服務電話:0371-60135900
      10. 專注服務器托管17年
        掃掃關注-微信公眾號
        0371-60135900
        Copyright© 1999-2019 ENKJ All Rights Reserved 億恩科技 版權所有  地址:鄭州市高新區翠竹街1號總部企業基地億恩大廈  法律顧問:河南亞太人律師事務所郝建鋒、杜慧月律師   京公網安備41019702002023號
          0
         
         
         
         

        0371-60135900
        7*24小時客服服務熱線

         
         
        av不卡不卡在线观看_最近2018年中文字幕_亚洲欧美一区二区三区_一级A爱做片免费观看国产_日韩在线中文天天更新_伊人中文无码在线