Add hacky async-as-sync helpers

This commit is contained in:
Yoshi Askharoun
2023-05-07 21:56:04 -05:00
parent 1844ea71eb
commit 5d801a7166
2 changed files with 43 additions and 21 deletions
+26
View File
@@ -0,0 +1,26 @@
using CommunityToolkit.Diagnostics;
using System;
using System.Threading.Tasks;
namespace Microsoft.Zune;
public static class AsyncHelper
{
public static void Run(Task task)
{
if (task == null)
return;
var t = Task.Run(() => task);
t.Wait();
}
public static TResult Run<TResult>(Func<Task<TResult>> action)
{
Guard.IsNotNull(action);
var t = Task.Run(action);
t.Wait();
return t.Result;
}
}
+5 -9
View File
@@ -1,25 +1,22 @@
using System;
using System.IO;
using System.Threading;
#if NETSTANDARD1_0_OR_GREATER
using System.Net.Http;
using System.Threading.Tasks;
#endif
namespace Microsoft.Zune
namespace Microsoft.Zune;
internal static class Extensions
{
internal static class Extensions
{
#if NETSTANDARD2_0_OR_GREATER && !NET5_0_OR_GREATER
public static Stream ReadAsStream(this HttpContent content)
{
return Task.Run(content.ReadAsStreamAsync).Result;
return AsyncHelper.Run(content.ReadAsStreamAsync);
}
public static HttpResponseMessage Send(this HttpClient client, HttpRequestMessage request)
{
return Task.Run(() => client.SendAsync(request)).Result;
return AsyncHelper.Run(() => client.SendAsync(request));
}
public static Task<Stream> GetStreamAsync(this HttpClient client, Uri uri, CancellationToken _)
@@ -27,5 +24,4 @@ namespace Microsoft.Zune
return client.GetStreamAsync(uri);
}
#endif
}
}