diff --git a/.editorconfig b/.editorconfig index 09b904a73f..df25de13a1 100644 --- a/.editorconfig +++ b/.editorconfig @@ -13,9 +13,6 @@ dotnet_diagnostic.VSTHRD100.severity = none # VSTHRD103: Call async methods when in an async method dotnet_diagnostic.VSTHRD103.severity = none -# VSTHRD105: Avoid method overloads that assume TaskScheduler.Current -dotnet_diagnostic.VSTHRD105.severity = none - # VSTHRD200: Use "Async" suffix for async methods dotnet_diagnostic.VSTHRD200.severity = none diff --git a/src/Microsoft.ML.Core/Utilities/ResourceManagerUtils.cs b/src/Microsoft.ML.Core/Utilities/ResourceManagerUtils.cs index 4782b14479..046880b922 100644 --- a/src/Microsoft.ML.Core/Utilities/ResourceManagerUtils.cs +++ b/src/Microsoft.ML.Core/Utilities/ResourceManagerUtils.cs @@ -151,7 +151,7 @@ private async Task DownloadFromUrl(IHostEnvironment env, IChannel ch, st var t = Task.Run(() => DownloadResource(env, ch, webClient, new Uri(url), filePath, fileName, downloadCancel.Token)); UpdateTimeout(ref timeout); - var timeoutTask = Task.Delay(timeout).ContinueWith(task => default(Exception)); + var timeoutTask = Task.Delay(timeout).ContinueWith(task => default(Exception), TaskScheduler.Default); ch.Info($"Downloading {fileName} from {url} to {filePath}"); var completedTask = await Task.WhenAny(t, timeoutTask); if (completedTask != t || completedTask.Result != null) diff --git a/src/Microsoft.ML.FastTree/Training/TreeLearners/LeastSquaresRegressionTreeLearner.cs b/src/Microsoft.ML.FastTree/Training/TreeLearners/LeastSquaresRegressionTreeLearner.cs index 0b31a6bc72..d607bacf86 100644 --- a/src/Microsoft.ML.FastTree/Training/TreeLearners/LeastSquaresRegressionTreeLearner.cs +++ b/src/Microsoft.ML.FastTree/Training/TreeLearners/LeastSquaresRegressionTreeLearner.cs @@ -347,7 +347,7 @@ protected virtual void FindBestSplitOfRoot(double[] targets) using (Timer.Time(TimerEvent.FindBestSplit)) using (Timer.Time(TimerEvent.FindBestSplitOfRoot)) { - var smallSplitInit = Task.Factory.StartNew(() => + var smallSplitInit = Task.Run(() => { // Initialize. using (Timer.Time(TimerEvent.FindBestSplitInit)) @@ -408,7 +408,7 @@ protected virtual void FindBestSplitOfSiblings(int lteChild, int gtChild, Docume { using (Timer.Time(TimerEvent.FindBestSplitInit)) { - var smallSplitInit = Task.Factory.StartNew(() => SmallerChildSplitCandidates.Initialize(lteChild, partitioning, targets, GetTargetWeights(), FilterZeros)); + var smallSplitInit = Task.Run(() => SmallerChildSplitCandidates.Initialize(lteChild, partitioning, targets, GetTargetWeights(), FilterZeros)); LargerChildSplitCandidates.Initialize(gtChild, partitioning, targets, GetTargetWeights(), FilterZeros); smallSplitInit.Wait(); } @@ -421,7 +421,7 @@ protected virtual void FindBestSplitOfSiblings(int lteChild, int gtChild, Docume { using (Timer.Time(TimerEvent.FindBestSplitInit)) { - var smallSplitInit = Task.Factory.StartNew(() => SmallerChildSplitCandidates.Initialize(gtChild, partitioning, targets, GetTargetWeights(), FilterZeros)); + var smallSplitInit = Task.Run(() => SmallerChildSplitCandidates.Initialize(gtChild, partitioning, targets, GetTargetWeights(), FilterZeros)); LargerChildSplitCandidates.Initialize(lteChild, partitioning, targets, GetTargetWeights(), FilterZeros); smallSplitInit.Wait(); } diff --git a/test/Microsoft.ML.Benchmarks/ImageClassificationBench.cs b/test/Microsoft.ML.Benchmarks/ImageClassificationBench.cs index 7a77d31967..0e8512dde4 100644 --- a/test/Microsoft.ML.Benchmarks/ImageClassificationBench.cs +++ b/test/Microsoft.ML.Benchmarks/ImageClassificationBench.cs @@ -227,7 +227,7 @@ public class ImageData } public static class HttpContentExtensions { - public static Task ReadAsFileAsync(this HttpContent content, string filename, bool overwrite) + public static async Task ReadAsFileAsync(this HttpContent content, string filename, bool overwrite) { string pathname = Path.GetFullPath(filename); if (!overwrite && File.Exists(filename)) @@ -235,25 +235,8 @@ public static Task ReadAsFileAsync(this HttpContent content, string filename, bo throw new InvalidOperationException(string.Format("File {0} already exists.", pathname)); } - FileStream fileStream = null; - try - { - fileStream = new FileStream(pathname, FileMode.Create, FileAccess.Write, FileShare.None); - return content.CopyToAsync(fileStream).ContinueWith( - (copyTask) => - { - fileStream.Close(); - }); - } - catch - { - if (fileStream != null) - { - fileStream.Close(); - } - - throw; - } + using FileStream fileStream = new FileStream(pathname, FileMode.Create, FileAccess.Write, FileShare.None); + await content.CopyToAsync(fileStream); } } }