这段是对 Task 的扩展
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace CefSharp
{
public class TaskEx
{
public static Task<T> FromResult<T>(T t)
{
return Task.Factory.StartNew<T>(() => t);
}
public static Task Run(Action action)
{
var tcs = new TaskCompletionSource<object>();
new Thread(() => {
try
{
action();
tcs.SetResult(null);
}
catch (Exception ex)
{
tcs.SetException(ex);
}
})
{ IsBackground = true }.Start();
return tcs.Task;
}
public static Task<TResult> Run<TResult>(Func<TResult> function)
{
var tcs = new TaskCompletionSource<TResult>();
new Thread(() =>
{
try
{
tcs.SetResult(function());
}
catch (Exception ex)
{
tcs.SetException(ex);
}
})
{ IsBackground = true }.Start();
return tcs.Task;
}
public static Task Delay(int milliseconds)
{
var tcs = new TaskCompletionSource<object>();
var timer = new System.Timers.Timer(milliseconds) { AutoReset = false };
timer.Elapsed += delegate { timer.Dispose(); tcs.SetResult(null); };
timer.Start();
return tcs.Task;
}
}
}
把在C#内添加以上代码里, 遇到 Task.Run 的时候,替换成 TaskEx.Run,遇到 Task.Delay 替换为TaskEx.Delay.
还有报 GetTypeInfo 这个错误的地方,删之就Ok了。
以上这篇CefSharp v62修改方法(支持.net4.0)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易采站长站。








