-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathAsyncUtils.cs
More file actions
49 lines (39 loc) · 1.51 KB
/
AsyncUtils.cs
File metadata and controls
49 lines (39 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Copyright (c) ServiceStack, Inc. All Rights Reserved.
// License: https://raw.github.com/ServiceStack/ServiceStack/master/license.txt
/*
* Keep as much platform specific stuff here
*/
using System;
using System.IO;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
namespace ServiceStack
{
public interface ITimer : IDisposable
{
void Cancel();
}
public delegate void ProgressDelegate(long done, long total);
internal static class AsyncUtils
{
internal static HttpWebRequest CreateHttpWebRequest(this AsyncServiceClient client, string requestUri)
{
var webRequest = WebRequest.CreateHttp(requestUri);
PclExport.Instance.Config(webRequest);
if (client.StoreCookies)
{
PclExportClient.Instance.SetCookieContainer(webRequest, client);
}
if (!client.DisableAutoCompression)
{
PclExport.Instance.AddCompression(webRequest);
}
return webRequest;
}
private static readonly TaskFactory SyncTaskFactory = new TaskFactory(CancellationToken.None,
TaskCreationOptions.None, TaskContinuationOptions.None, TaskScheduler.Default);
public static void RunSync(Func<Task> task) => SyncTaskFactory.StartNew(task).Unwrap().GetAwaiter().GetResult();
public static TResult RunSync<TResult>(Func<Task<TResult>> task) => SyncTaskFactory.StartNew(task).Unwrap().GetAwaiter().GetResult();
}
}