private TimerElapsed ( object sender, System.Timers.ElapsedEventArgs e ) : void | ||
sender | object | |
e | System.Timers.ElapsedEventArgs | |
Résultat | void |
void TimerElapsed(object sender, ElapsedEventArgs e)
{
if (!this.isBusy)
{
return;
}
if (!this.IgnoreExpiresHeader)
{
// Since Expires is a static guess by the server, use Expires only when the server did
// not send back any Etag or LastModifiedTime, which makes conditional GET impossible
if (this.etag == null && this.lastModifiedTime == null && this.expires != null && (DateTime.UtcNow < this.expires.Value.ToUniversalTime()))
{
return;
}
}
this.expires = null;
HttpRequestMessage request = new HttpRequestMessage("GET", this.uri);
if (this.etag != null)
{
var ifNoneMatch = new HeaderValues<EntityTag>();
ifNoneMatch.Add(this.etag);
request.Headers.IfNoneMatch = ifNoneMatch;
}
request.Headers.IfModifiedSince = this.lastModifiedTime;
bool stopTimer = false;
try
{
HttpResponseMessage response = null;
try
{
response = this.HttpClient.Send(request);
}
catch (Exception ex)
{
if (!this.IgnoreSendErrors)
{
stopTimer = InvokeHandler(ex);
}
if (response != null)
{
response.Dispose();
}
return;
}
using (response)
{
switch (response.StatusCode)
{
case HttpStatusCode.NotModified:
// the resource has not been modified
response.Dispose();
break;
case HttpStatusCode.OK:
// the resource has been modified. Fire the event, along with the response message
this.etag = response.Headers.ETag;
this.expires = response.Headers.Expires;
this.lastModifiedTime = response.Headers.LastModified;
try
{
stopTimer = InvokeHandler(response);
}
finally
{
response.Dispose();
}
break;
default:
// this is an unexpected error. Fire the event, if errors are not to be suppressed
try
{
if (!this.IgnoreNonOKStatusCodes)
{
stopTimer = InvokeHandler(response);
}
}
finally
{
response.Dispose();
}
break;
}
}
}
finally
{
if (stopTimer)
{
StopPolling();
}
}
}