1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
//------------------------------------------------------------------------------
// <copyright file="TimeoutTimer.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//
// Class used to manage timeouts in complex system operations.
//
// <owner current="true" primary="true">Microsoft</owner>
// <owner current="true" primary="false">Microsoft</owner>
//------------------------------------------------------------------------------
namespace System.Data.ProviderBase
{
using System;
using System.Data.Common;
using System.Diagnostics;
// Purpose:
// Manages determining and tracking timeouts
//
// Intended use:
// Call StartXXXXTimeout() to get a timer with the given expiration point
// Get remaining time in appropriate format to pass to subsystem timeouts
// Check for timeout via IsExpired for checks in managed code.
// Simply abandon to GC when done.
internal class TimeoutTimer
{
//-------------------
// Fields
//-------------------
private long _timerExpire;
private bool _isInfiniteTimeout;
//-------------------
// Timeout-setting methods
//-------------------
// Get a new timer that will expire in the given number of seconds
// For input, a value of zero seconds indicates infinite timeout
internal static TimeoutTimer StartSecondsTimeout(int seconds)
{
//--------------------
// Preconditions: None (seconds must conform to SetTimeoutSeconds requirements)
//--------------------
// Method body
var timeout = new TimeoutTimer();
timeout.SetTimeoutSeconds(seconds);
//---------------------
// Postconditions
Debug.Assert(timeout != null); // Need a valid timeouttimer if no error
return timeout;
}
// Get a new timer that will expire in the given number of milliseconds
// No current need to support infinite milliseconds timeout
internal static TimeoutTimer StartMillisecondsTimeout(long milliseconds)
{
//--------------------
// Preconditions
Debug.Assert(0 <= milliseconds);
//--------------------
// Method body
var timeout = new TimeoutTimer();
timeout._timerExpire = checked(ADP.TimerCurrent() + (milliseconds * TimeSpan.TicksPerMillisecond));
timeout._isInfiniteTimeout = false;
//---------------------
// Postconditions
Debug.Assert(timeout != null); // Need a valid timeouttimer if no error
return timeout;
}
//-------------------
// Methods for changing timeout
//-------------------
internal void SetTimeoutSeconds(int seconds)
{
//--------------------
// Preconditions
Debug.Assert(0 <= seconds || InfiniteTimeout == seconds); // no need to support negative seconds at present
//--------------------
// Method body
if (InfiniteTimeout == seconds)
{
_isInfiniteTimeout = true;
}
else
{
// Stash current time + timeout
_timerExpire = checked(ADP.TimerCurrent() + ADP.TimerFromSeconds(seconds));
_isInfiniteTimeout = false;
}
//---------------------
// Postconditions:None
}
//-------------------
// Timeout info properties
//-------------------
// Indicator for infinite timeout when starting a timer
internal static readonly long InfiniteTimeout = 0;
// Is this timer in an expired state?
internal bool IsExpired
{
get
{
return !IsInfinite && ADP.TimerHasExpired(_timerExpire);
}
}
// is this an infinite-timeout timer?
internal bool IsInfinite
{
get
{
return _isInfiniteTimeout;
}
}
// Special accessor for TimerExpire for use when thunking to legacy timeout methods.
internal long LegacyTimerExpire
{
get
{
return (_isInfiniteTimeout) ? Int64.MaxValue : _timerExpire;
}
}
// Returns milliseconds remaining trimmed to zero for none remaining
// and long.MaxValue for infinite
// This method should be prefered for internal calculations that are not
// yet common enough to code into the TimeoutTimer class itself.
internal long MillisecondsRemaining
{
get
{
//-------------------
// Preconditions: None
//-------------------
// Method Body
long milliseconds;
if (_isInfiniteTimeout)
{
milliseconds = long.MaxValue;
}
else
{
milliseconds = ADP.TimerRemainingMilliseconds(_timerExpire);
if (0 > milliseconds)
{
milliseconds = 0;
}
}
//--------------------
// Postconditions
Debug.Assert(0<=milliseconds); // This property guarantees no negative return values
return milliseconds;
}
}
}
}
|