Hi Nick,
It costs me some time to read all your discussions with the community
before I can provide a reply to you.
I am not sure if I understand you completely. Based on my understanding,
you are writting a stress simulator application for SQL Server. After
openning a connection to the SQL Server, the thread uses Thread.Sleep()
method to simulate 30 second *processing time*(think time). However, every
20 seconds or so the output of your routine would pause for about 10
seconds, so you wanted to find out the root cause of this problem. If I
have misunderstood you, please feel free to tell me, thanks.
The point that I do not understand well is what "output of your routine
pause for 10 seconds" means. Can you tell me what is this "routine"? How
does the routine generate output?
Another point:
Sleep() continuing to use the thread's quanta I decided to use a wait function to see
if that would produce any better results
I think I do not understand your expected behavior completely. Can you
telll me what is the "better result" behavior? Then I will judge to see why
Thread.Sleep() does not meet your need. Thanks.
Anyway, I wanted to share some information with you:
1. .Net2.0 System.Threading.Thread.Sleep() method internally calls Win32
SleepEx() API. I can prove this by setting a breakpoint in Kernel32!SleepEx
API in windbg for a .Net2.0 Winform application. Below is the call stack I
got while I call Thread.Sleep() method in .Net:
0:000> !dumpstack
Current frame: KERNEL32!SleepEx
ChildEBP RetAddr Caller,Callee
0013f230 792144fc mscorwks!Thread::UserSleep+0x93, calling KERNEL32!SleepEx
0013f250 79214574 mscorwks!ThreadNative::Sleep+0x30, calling
mscorwks!Thread::UserSleep
0013f260 011a03a6 011a03a6
0013f28c 79bbaeb0 (stub for System.Threading.Thread.Sleep), calling
011a037c
0013f290 0111026f (MethodDesc 0xc253c8 +0x17
winformtest.Form1.button1_Click), calling 79bbaeab (MethodDesc 0x79bbaeb0
System.Threading.Thread.Sleep)
0013f2a0 7b881fa4 (MethodDesc 0x7b9e5108 +0x54
System.Windows.Forms.Control.OnClick)
If you want to know more detailed information, you may download SSCLI2.0
project to view the sample source code of ThreadNative::Sleep,
Thread::UserSleep of CLR.
"Shared Source Common Language Infrastructure 2.0 Release"
http://www.microsoft.com/downloads/details.aspx?FamilyId=8C09FD61-3F26-4555-
AE17-3121B4F51D4D&displaylang=en
2. In Windows thread schedulling, any wait operation will be decreased 1
point of quantum unit. And the default thread quantum has about 3 quantum
units to run. So after Sleep(30000) calling, the calling thread will go
into wait state for 30 seconds, then its quantum will reduce 1 unit. Note:
once in wait state, this thread can not be scheduled for execution, other
thread will execute. Also, after wakeup, the calling thread will not run
immediately, it will be placed in the read thread queue for scheduling.
For event/semaphore, it will normally behaves the same as Thread.Sleep().
However, after wakeup, Windows will provide a special priority boost to the
calling thread, so that the wakeup thread can run immediately. I am not
sure if this difference is the root cause of your problem.
3. If you think your simulator application is hanging for 10 seconds, the
best technology to troubshoot is using "Process Explorer" to examine what
each thread is hanging about. You may just examine each thread call stack
during this 10 seconds hang. The call stack will reveal what's going on.
The following article may not be very relevant to your question, but it
demonstrates how to troubleshoot the hang/delay style problem:
http://www.codeproject.com/system/NoDeleteDelay.asp
Finally, the Windows thread scheduling is not a trivial topic, it is
covered best in Chapter 6 of Mark E. Russinovich's wonderful book <Windows
Internals>. More specificly, "Thread Scheduling" section is dedicated for
this topic. You may give it a reading to understand more background
information.
Hope this helps.
Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.