예제 #1
0
        async Task <Stream> GetStreamFromCacheAsync(Uri uri, CancellationToken cancellationToken)
        {
            string           key = GetCacheKey(uri);
            LockingSemaphore sem;

            lock (s_syncHandle)
            {
                if (s_semaphores.ContainsKey(key))
                {
                    sem = s_semaphores[key];
                }
                else
                {
                    s_semaphores.Add(key, sem = new LockingSemaphore(1));
                }
            }

            try
            {
                await sem.WaitAsync(cancellationToken);

                Stream stream = await GetStreamAsyncUnchecked(key, uri, cancellationToken);

                if (stream == null)
                {
                    sem.Release();
                    return(null);
                }
                var wrapped = new StreamWrapper(stream);
                wrapped.Disposed += (o, e) => sem.Release();
                return(wrapped);
            }
            catch (OperationCanceledException)
            {
                sem.Release();
                throw;
            }
        }
예제 #2
0
		async Task<Stream> GetStreamFromCacheAsync(Uri uri, CancellationToken cancellationToken)
		{
			string key = GetCacheKey(uri);
			LockingSemaphore sem;
			lock (s_syncHandle)
			{
				if (s_semaphores.ContainsKey(key))
					sem = s_semaphores[key];
				else
					s_semaphores.Add(key, sem = new LockingSemaphore(1));
			}

			try
			{
				await sem.WaitAsync(cancellationToken);
				Stream stream = await GetStreamAsyncUnchecked(key, uri, cancellationToken);
				if (stream == null)
				{
					sem.Release();
					return null;
				}
				var wrapped = new StreamWrapper(stream);
				wrapped.Disposed += (o, e) => sem.Release();
				return wrapped;
			}
			catch (OperationCanceledException)
			{
				sem.Release();
				throw;
			}
		}