Just for fun…
1: public class MinimumExecuteTimer
2: : IDisposable
3: {
4: private Stopwatch Stopwatch { get; set; }
5: private TimeSpan MinimumExecutionTime { get; set; }
6:
7: public MinimumExecuteTimer(TimeSpan minimumExecuteTime)
8: {
9: MinimumExecutionTime = minimumExecuteTime;
10: Stopwatch = new Stopwatch();
11: Stopwatch.Start();
12: }
13:
14: #region IDisposable Members
15:
16: public void Dispose()
17: {
18: Stopwatch.Stop();
19: TimeSpan sleepTime = MinimumExecutionTime.Subtract(Stopwatch.Elapsed);
20:
21: if (sleepTime.TotalMilliseconds > 0)
22: {
23: Thread.Sleep(sleepTime);
24: }
25: }
26:
27: #endregion
28: }
So, now I can ensure an operation will always take at least XX time to execute.
1: public void DoWork()
2: {
3: using(new MinimumExecuteTimer(TimeSpan.FromSeconds(10)))
4: {
5: // do something, block will hang
6: // for the time remaining
7: }
8: }
“Why would you do this in production?” you ask…
Good question, none that I can think of right now. I wrote the class to test what a Login form would look like while validating to a service call that took extremely long to execute.
No comments:
Post a Comment