From b838c5301787c181863436d4fc29ae1d61e25522 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Mon, 15 Apr 2013 15:09:27 -0400 Subject: [PATCH] reduce uses of Task.Run --- .../Drawing/ImageManager.cs | 6 +- .../HttpServer/HttpServer.cs | 82 +++++++++---------- .../Library/LibraryManager.cs | 2 +- .../Sqlite/SQLiteUserRepository.cs | 15 ++-- .../WebSocket/AlchemyWebSocket.cs | 2 +- 5 files changed, 50 insertions(+), 57 deletions(-) diff --git a/MediaBrowser.Controller/Drawing/ImageManager.cs b/MediaBrowser.Controller/Drawing/ImageManager.cs index ad3f66d2c7..32ff940b2b 100644 --- a/MediaBrowser.Controller/Drawing/ImageManager.cs +++ b/MediaBrowser.Controller/Drawing/ImageManager.cs @@ -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 /// /// The cache file path. /// The bytes. - 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 }; } diff --git a/MediaBrowser.Server.Implementations/HttpServer/HttpServer.cs b/MediaBrowser.Server.Implementations/HttpServer/HttpServer.cs index 4c7dd1da6c..87a01ef7d9 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/HttpServer.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/HttpServer.cs @@ -282,58 +282,54 @@ namespace MediaBrowser.Server.Implementations.HttpServer return; } + RaiseReceiveWebRequest(context); - Task.Run(() => + try { - RaiseReceiveWebRequest(context); - - try - { - ProcessRequest(context); - } - catch (InvalidOperationException ex) - { - HandleException(context.Response, ex, 422); + 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; + } } /// diff --git a/MediaBrowser.Server.Implementations/Library/LibraryManager.cs b/MediaBrowser.Server.Implementations/Library/LibraryManager.cs index 3ec881499f..6436e77167 100644 --- a/MediaBrowser.Server.Implementations/Library/LibraryManager.cs +++ b/MediaBrowser.Server.Implementations/Library/LibraryManager.cs @@ -82,7 +82,7 @@ namespace MediaBrowser.Server.Implementations.Library { UpdateLibraryCache(args); - EventHelper.QueueEventIfNotNull(LibraryChanged, this, args, _logger); + EventHelper.FireEventIfNotNull(LibraryChanged, this, args, _logger); } #endregion diff --git a/MediaBrowser.Server.Implementations/Sqlite/SQLiteUserRepository.cs b/MediaBrowser.Server.Implementations/Sqlite/SQLiteUserRepository.cs index f55b13d196..75bb42774f 100644 --- a/MediaBrowser.Server.Implementations/Sqlite/SQLiteUserRepository.cs +++ b/MediaBrowser.Server.Implementations/Sqlite/SQLiteUserRepository.cs @@ -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); } } } diff --git a/MediaBrowser.Server.Implementations/WebSocket/AlchemyWebSocket.cs b/MediaBrowser.Server.Implementations/WebSocket/AlchemyWebSocket.cs index 7bf1fe86d0..0b6b145665 100644 --- a/MediaBrowser.Server.Implementations/WebSocket/AlchemyWebSocket.cs +++ b/MediaBrowser.Server.Implementations/WebSocket/AlchemyWebSocket.cs @@ -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 {