下載 TimeDate.Now 的第一次是例如 15:57 所以我 RoundDown 使 15:55 格式化并嘗試下載它。
如果在完成的事件中沒有成功,我會嘗試再次從 15:55 到 15:50 格式化并嘗試再次下載。
問題是它不會再次四舍五入。在完成的事件中,如果有錯誤這一行:
current = RoundDown(current, TimeSpan.FromMinutes(-5));
它仍然是上面的四舍五入的 15:55,我希望在下載不成功之前保持四舍五入并嘗試下載新的向下舍入的新 currentLink。15:50 不成功 15:45 不成功 15:40 以此類推,一遍又一遍地構建currentLink,直到下載成功。
public void GetImages()
{
defaultlink = "https://IMSRadar/IMSRadar_";
current = RoundDown(DateTime.Now, TimeSpan.FromMinutes(-5));
var ct = current.ToString("yyyyMMddHHmm");
currentLink = defaultlink ct ".gif";
using (System.Net.WebClient wc = new System.Net.WebClient())
{
wc.DownloadFileCompleted = Wc_DownloadFileCompleted;
wc.DownloadFileAsync(new Uri(currentLink), @"d:\test.gif");
}
}
private void Wc_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
if (e.Error != null)
{
using (System.Net.WebClient wc = new System.Net.WebClient())
{
current = RoundDown(current, TimeSpan.FromMinutes(-5));
var ct = current.ToString("yyyyMMddHHmm");
currentLink = defaultlink ct ".gif";
wc.DownloadFileCompleted = Wc_DownloadFileCompleted;
wc.DownloadFileAsync(new Uri(currentLink), @"d:\test.gif");
}
}
else
{
GenerateRadarLinks();
}
}
方法 RoundDown
DateTime RoundDown(DateTime date, TimeSpan interval)
{
return new DateTime(date.Ticks / interval.Ticks *
interval.Ticks);
}
uj5u.com熱心網友回復:
roundDown一旦您到達某個日期(已經是 5 分鐘的倍數),您將無法作業。想象一下:
假設整數除法103 / 5 * 5會給20 * 5因此100如預期。但是在下一次迭代中,您100 / 5 * 5將再次給出100,因為
100 / 5 == 103 / 5 == 20(使用整數除法)。
因此,要使其正常作業,您需要將時間至少減少一個刻度,因此它不再是 5 分鐘的倍數。
DateTime RoundDown(DateTime date, TimeSpan interval) {
return new DateTime((date.Ticks - 1)/ interval.Ticks * interval.Ticks);
}
我個人只會使用該RoundDown方法一次來生成第一個時間戳,它是 5 的倍數。對于所有后續請求,我只使用當前時間戳并將其遞減 5 分鐘。
//inital timestamp
current = RoundDown(DateTime.Now, TimeSpan.FromMinutes(-5));
//in the error handler
current = current.AddMinutes(-5);
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/317066.html
上一篇:(在引數中傳遞“all”?)|我如何制作引數?在WHERE過濾器中回傳ALL在sql請求中
下一篇:從兩行獲取字串的正則運算式模式
