5

I want to introduce a slight wait during some testing functions, to simulate a server call. Is it sane to use Thread.Sleep(int) to introduce the wait or is there a better method to have the server wait?

Note, I'll be pausing long enough for me to visually see a sufficient lag, even tho I don't expect to see such a delay in the actual app. This is for me to visualize the actual delay that could occur.

I plan on running this both in the VS2010 local debugger webserver and in IIS 7. I'm on .NET 3.5

jcolebrand
  • 15,889
  • 12
  • 75
  • 121

3 Answers3

6

Thread.Sleep(int) is what I would use.

The Muffin Man
  • 19,585
  • 30
  • 119
  • 191
2

If you're using MVC and SessionState your requests will be automatially serialized - so if you're checking for race conditions with a random Thread.Sleep() value then Request B will never complete before Request A even if the time slept if less.

ASP.NET MVC and Ajax, concurrent requests?

Community
  • 1
  • 1
Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689
1

This is perfectly fine for your test sceanrio. As long as you are not doing stress/load testing or calls to services are synchronous replacing them with simple Sleep gives close approximation of server behavior.

If your code uses asynchronous service calls you may want to emulate slightly more of the service calls and put Sleeps on separate threads as if you call service, but it took long time. This way ASP.Net will behave normally and you may even try some load testing.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • When you say "simple Sleep" do you mean "Thread.Sleep(int)" as I gave the example or do you mean something else? – jcolebrand Mar 16 '11 at 22:12
  • @drachenstern, I sense that you are hoping that there is a `Thread.Dream(int)` in addition to `Thread.Sleep(int)`. How many different ways do you need to pause? – The Muffin Man Mar 16 '11 at 22:45
  • @Nick ~ No, I want to make sure that there's not something better for ASP.NET on account of the threadpool model where it could have impacts I'm not considering. – jcolebrand Mar 16 '11 at 23:29