reduce uses of Task.Run

This commit is contained in:
Luke Pulverenti 2013-04-15 15:09:27 -04:00
parent 2b8b98b590
commit b838c53017
5 changed files with 50 additions and 57 deletions

View File

@ -217,7 +217,7 @@ namespace MediaBrowser.Controller.Drawing
var outputTask = toStream.WriteAsync(bytes, 0, bytes.Length);
// kick off a task to cache the result
Task.Run(() => CacheResizedImage(cacheFilePath, bytes));
await CacheResizedImage(cacheFilePath, bytes).ConfigureAwait(false);
await outputTask.ConfigureAwait(false);
}
@ -238,7 +238,7 @@ namespace MediaBrowser.Controller.Drawing
/// </summary>
/// <param name="cacheFilePath">The cache file path.</param>
/// <param name="bytes">The bytes.</param>
private async void CacheResizedImage(string cacheFilePath, byte[] bytes)
private async Task CacheResizedImage(string cacheFilePath, byte[] bytes)
{
// Save to the cache location
using (var cacheFileStream = new FileStream(cacheFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous))
@ -320,7 +320,7 @@ namespace MediaBrowser.Controller.Drawing
var size = ImageHeader.GetDimensions(imagePath, _logger);
// Update the file system cache
Task.Run(() => File.WriteAllText(fullCachePath, size.Width.ToString(UsCulture) + @"|" + size.Height.ToString(UsCulture)));
File.WriteAllText(fullCachePath, size.Width.ToString(UsCulture) + @"|" + size.Height.ToString(UsCulture));
return new ImageSize { Width = size.Width, Height = size.Height };
}

View File

@ -282,58 +282,54 @@ namespace MediaBrowser.Server.Implementations.HttpServer
return;
}
RaiseReceiveWebRequest(context);
Task.Run(() =>
try
{
RaiseReceiveWebRequest(context);
ProcessRequest(context);
}
catch (InvalidOperationException ex)
{
HandleException(context.Response, ex, 422);
try
{
ProcessRequest(context);
}
catch (InvalidOperationException ex)
{
HandleException(context.Response, ex, 422);
throw;
}
catch (ResourceNotFoundException ex)
{
HandleException(context.Response, ex, 404);
throw;
}
catch (ResourceNotFoundException ex)
{
HandleException(context.Response, ex, 404);
throw;
}
catch (FileNotFoundException ex)
{
HandleException(context.Response, ex, 404);
throw;
}
catch (FileNotFoundException ex)
{
HandleException(context.Response, ex, 404);
throw;
}
catch (DirectoryNotFoundException ex)
{
HandleException(context.Response, ex, 404);
throw;
}
catch (DirectoryNotFoundException ex)
{
HandleException(context.Response, ex, 404);
throw;
}
catch (UnauthorizedAccessException ex)
{
HandleException(context.Response, ex, 401);
throw;
}
catch (UnauthorizedAccessException ex)
{
HandleException(context.Response, ex, 401);
throw;
}
catch (ArgumentException ex)
{
HandleException(context.Response, ex, 400);
throw;
}
catch (ArgumentException ex)
{
HandleException(context.Response, ex, 400);
throw;
}
catch (Exception ex)
{
HandleException(context.Response, ex, 500);
throw;
}
catch (Exception ex)
{
HandleException(context.Response, ex, 500);
throw;
}
});
throw;
}
}
/// <summary>

View File

@ -82,7 +82,7 @@ namespace MediaBrowser.Server.Implementations.Library
{
UpdateLibraryCache(args);
EventHelper.QueueEventIfNotNull(LibraryChanged, this, args, _logger);
EventHelper.FireEventIfNotNull(LibraryChanged, this, args, _logger);
}
#endregion

View File

@ -195,17 +195,14 @@ namespace MediaBrowser.Server.Implementations.Sqlite
throw new ArgumentNullException("cancellationToken");
}
return Task.Run(() =>
{
cancellationToken.ThrowIfCancellationRequested();
cancellationToken.ThrowIfCancellationRequested();
var cmd = connection.CreateCommand();
cmd.CommandText = "delete from users where guid=@guid";
var guidParam = cmd.Parameters.Add("@guid", DbType.Guid);
guidParam.Value = user.Id;
var cmd = connection.CreateCommand();
cmd.CommandText = "delete from users where guid=@guid";
var guidParam = cmd.Parameters.Add("@guid", DbType.Guid);
guidParam.Value = user.Id;
return ExecuteCommand(cmd);
});
return ExecuteCommand(cmd);
}
}
}

View File

@ -1,11 +1,11 @@
using Alchemy.Classes;
using MediaBrowser.Common.Net;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Net;
using System;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Model.Net;
namespace MediaBrowser.Server.Implementations.WebSocket
{