Search code examples
c#multithreadingwcfthreadpool

Is the thread blocked when doing database operation?


We have a WCF service (deployed on IIS) that makes expensive calls to database. It doesn't use any kind of asynchronous programming (async / await).

using (SqlDataAdapter adapter = new SqlDataAdapter())
{
    using (SqlCommand command = new SqlCommand())
    {
        command.CommandType = CommandType.StoredProcedure;
        command.CommandText = "StoredProcedure_NAME";   // takes 6 seconds
        command.Connection = connection;
        adapter.SelectCommand = command;
        adapter.Fill(dataset);
        return dataset;
    }
}

Now, what will happen to the thread (I believe this is a WCF thread pool thread) making this call to the database during the 6 seconds of wait time. Is this thread blocking and taking up CPU cycles or will thread be in waiting state (waiting for an interrupt)?


Solution

  • Yes, it is blocked. There are async methods that are designed to return the thread to the listening pool, and retrieve a new (or the same) thread from the thread pool when completion of the task occurs. Non async methods will block your calling thread.