Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've read many articles say that IOCP is used in BeginXX/EndXX pair calls.
However, when i test them, my result shown that IOCP didn't work in BeginExecuteReader call, while it worked just fine in BeginGetResponse call.
I'm very confused with this outcome.
Can anyone tell me the reason?
Is there anything wrong with my test code?
Here's the test below:

1. test with BeginGetResponse
Code:

C#
public static void IoThread2()
{
      ThreadPool.SetMinThreads(5, 3);
      ThreadPool.SetMaxThreads(5, 3);
      int w; int io;
      ThreadPool.GetAvailableThreads(out w, out io);
      int cid = Thread.CurrentThread.ManagedThreadId;
      Console.WriteLine("Begin:" + w.ToString() + ";" + io.ToString() + "; id = " + cid.ToString());
      ManualResetEvent waitHandle = new ManualResetEvent(false);
      WebRequest request = HttpWebRequest.Create("http://www.cnblogs.com/");
      AsyncCallback cb = new AsyncCallback(IOThread2CallBack);
      request.BeginGetResponse(cb, request);
      waitHandle.WaitOne();
}

public static void IOThread2CallBack(IAsyncResult ar)
{
     try
     {
         WebRequest request = (WebRequest)ar.AsyncState;
         int w2; int io2;
         ThreadPool.GetAvailableThreads(out w2, out io2);
         int cid2 = Thread.CurrentThread.ManagedThreadId;
         Console.WriteLine("End:" + w2.ToString() + ";" + io2.ToString() + "; id = " + cid2.ToString());
         var response = request.EndGetResponse(ar);
      }catch (Exception ex){ }
}


Result:
Begin:5;3; id = 10
End:5;2; id = 13
one io thread was used to execute the callback

2. test with BeginExecuteReader
Code:

C#
public static void IoThread1()
{
     ThreadPool.SetMinThreads(5, 3);
     ThreadPool.SetMaxThreads(5, 3);
     int w; int io;
     ThreadPool.GetAvailableThreads(out w, out io);
     int cid = Thread.CurrentThread.ManagedThreadId;
     Console.WriteLine("Begin:" + w.ToString() + ";" + io.ToString() + "; id = " + cid.ToString());
     SqlConnection connection = new SqlConnection(connectionString);
     connection.Open();
     AsyncCallback da = new AsyncCallback(IoThreadCallBack);
     SqlCommand command = new SqlCommand(s_QueryDatabaseListScript, connection);
     IAsyncResult ir = command.BeginExecuteReader(da, command,System.Data.CommandBehavior.CloseConnection);
     ManualResetEvent waitHandle = new ManualResetEvent(false);
     waitHandle.WaitOne();
}
public static void IoThreadCallBack(IAsyncResult ar)
{
     int w; int io;
     ThreadPool.GetAvailableThreads(out w, out io);
     int cid = Thread.CurrentThread.ManagedThreadId;
     Console.WriteLine("End:" + w.ToString() + ";" + io.ToString() + "; id = " + cid.ToString());
     SqlCommand command = (SqlCommand)ar.AsyncState;
     StringBuilder sb = new StringBuilder();
     try
     {
           using (SqlDataReader reader = command.EndExecuteReader(ar))
           {
                while (reader.Read())
                {
                    sb.Append(reader.GetString(0)).Append("; ");
                }
           }
      }
      catch (Exception ex){ }
      finally
      {
           command.Connection.Close();
      }
}


Result:
Begin:5;3; id = 10
End:4;3; id = 7
another work thread was used to execute the callback
what's the problem....
Posted
Updated 30-Mar-12 10:22am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900