Beispiel #1
0
		private static async Task RequestImagePathForCalendarStartRequestAsync(BingImageInfo info, RangeRequestInfo rangeInfo)
		{
			DateTime date;
			string path = null;
			Debug.WriteLine("Task for " + info.StartDate + " started");
			if (!BingDataHelper.TryConvertStartdate(info.StartDate, out date))
			{
				rangeInfo.Callback(date, false, null);
				return;
			}

			if (RequestImagePathCheckCache(info.StartDate, out path))
			{
				rangeInfo.Callback(date, true, path);
				return;
			}

			await calendarRequestSemaphore.WaitAsync();

			try
			{
				if (rangeInfo.IsCancelled) return;
				var fileName = info.StartDate + ".jpg";
				var filePath = Path.Combine(Setting.GetCurrentSetting().TempPath.LocalPath, fileName);
				if (File.Exists(filePath)) File.Delete(filePath);

				// see if we can grab the 1080 source.  if failed, try to get the 1366 source
				if (await RequestImagePathTryBingApiAsync(new Uri(BingBaseUri, info.Url), date, rangeInfo, filePath))
				{
					if (rangeInfo.IsCancelled) return;
					if (RequestImagePathCheckCache(info.StartDate, out path))
					{
						rangeInfo.Callback(date, true, path);
						return;
					}
				}
				else if (await RequestImagePathTryBingApiAsync(new Uri(BingBaseUri, info.UrlBase + "_1366x768.jpg"), date, rangeInfo, filePath))
				{
					if (rangeInfo.IsCancelled) return;
					if (RequestImagePathCheckCache(info.StartDate, out path))
					{
						rangeInfo.Callback(date, true, path);
						return;
					}
				}
			}
			catch (Exception e)
			{
				// any exception cause the thread to fail
				Debug.WriteLine(e.Message);
			}
			finally
			{
				rangeInfo.RemoveRequest(date);
				calendarRequestSemaphore.Release();
			}
			if (!rangeInfo.IsCancelled) rangeInfo.Callback(date, false, null);
		}