mirror of
https://github.com/ZuneDev/ZuneShell.dll.git
synced 2026-07-27 13:11:51 -07:00
Add hacky async-as-sync helpers
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
+17
-21
@@ -1,31 +1,27 @@
|
||||
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;
|
||||
}
|
||||
|
||||
public static HttpResponseMessage Send(this HttpClient client, HttpRequestMessage request)
|
||||
{
|
||||
return Task.Run(() => client.SendAsync(request)).Result;
|
||||
}
|
||||
|
||||
public static Task<Stream> GetStreamAsync(this HttpClient client, Uri uri, CancellationToken _)
|
||||
{
|
||||
return client.GetStreamAsync(uri);
|
||||
}
|
||||
#endif
|
||||
public static Stream ReadAsStream(this HttpContent content)
|
||||
{
|
||||
return AsyncHelper.Run(content.ReadAsStreamAsync);
|
||||
}
|
||||
|
||||
public static HttpResponseMessage Send(this HttpClient client, HttpRequestMessage request)
|
||||
{
|
||||
return AsyncHelper.Run(() => client.SendAsync(request));
|
||||
}
|
||||
|
||||
public static Task<Stream> GetStreamAsync(this HttpClient client, Uri uri, CancellationToken _)
|
||||
{
|
||||
return client.GetStreamAsync(uri);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user