chore: 调整命名空间

oneRain 2020-04-28 17:03:50 +08:00
parent a06b836a10
commit 7380cdfdc2
3 changed files with 45 additions and 2 deletions

View File

@ -1,4 +1,4 @@
namespace LeanCloud.Common { namespace LeanCloud {
/// <summary> /// <summary>
/// 日志级别 /// 日志级别
/// </summary> /// </summary>

View File

@ -1,7 +1,7 @@
using System; using System;
using System.Text; using System.Text;
namespace LeanCloud.Common { namespace LeanCloud {
/// <summary> /// <summary>
/// 日志类 /// 日志类
/// </summary> /// </summary>

View File

@ -0,0 +1,43 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Concurrent;
using System.Collections.Generic;
namespace LeanCloud.Common {
/// <summary>
/// 单线程环境,用于控制台应用 await 返回
/// </summary>
public class SingleThreadSynchronizationContext : SynchronizationContext {
private readonly BlockingCollection<KeyValuePair<SendOrPostCallback, object>> queue = new BlockingCollection<KeyValuePair<SendOrPostCallback, object>>();
public override void Post(SendOrPostCallback d, object state) {
queue.Add(new KeyValuePair<SendOrPostCallback, object>(d, state));
}
public void RunOnCurrentThread() {
while (queue.TryTake(out KeyValuePair<SendOrPostCallback, object> workItem, Timeout.Infinite)) {
workItem.Key(workItem.Value);
}
}
public void Complete() {
queue.CompleteAdding();
}
public static void Run(Func<Task> func) {
SynchronizationContext prevContext = Current;
try {
SingleThreadSynchronizationContext syncContext = new SingleThreadSynchronizationContext();
SetSynchronizationContext(syncContext);
Task t = func();
syncContext.RunOnCurrentThread();
t.GetAwaiter().GetResult();
} finally {
SetSynchronizationContext(prevContext);
}
}
}
}