Skip to content

Enable VSTHRD105 (Avoid method overloads that assume TaskScheduler.Current) #4793

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.ML.Core/Utilities/ResourceManagerUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ private async Task<string> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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();
}
Expand All @@ -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();
}
Expand Down
23 changes: 3 additions & 20 deletions test/Microsoft.ML.Benchmarks/ImageClassificationBench.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,33 +227,16 @@ 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))
{
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);
}
}
}