博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CefSharp v62修改,支持.net4.0
阅读量:5261 次
发布时间:2019-06-14

本文共 8569 字,大约阅读时间需要 28 分钟。

吐槽一下,博客园久了没有上,账号没了,重新申请一个.

    cesharp v62版本,内核采用最新的Cef 62,支持最新的Grid布局. 由于官方的cefsharp 采用.net4.5.2开发.怎么办怎么办.我只能用.net4.0.没办法啊,自己拿源码修改兼容呗.

    仔细分析源码发现:

     1.net4.5.2 引入了 async/await 关键字. 这个其实国外大神已经有源码放出来了,我们把代码直接引入cefsharp 这个工程. 就可以直接在4.0里使用 async/await;

     2.net4.5 对task api 做了扩展, 我们只需要在.net4.0实现一下对应的api.就可以了.

    3. 源码里面用了很多4.5才有的GetTypeInfo      扩展方法错误. 它返回的类型是typeinfo,不用管它,把GetTypeInfo      删掉. 直接Type  调用就可以了.

    4. 对Task静态方法的扩展,需要修改一下,静态方法的调用方式.

以上是要点.下面贴源码:

本段源码是对:async/await的支持:

1 namespace System.Threading.Tasks  2 {  3     public static class TaskEx  4     {  5         public static TaskAwaiter GetAwaiter(this Task task)  6         {  7             return new TaskAwaiter(task);  8         }  9  10         public static TaskAwaiter
GetAwaiter
(this Task
task) 11 { 12 return new TaskAwaiter
(task); 13 } 14 } 15 16 public struct TaskAwaiter : INotifyCompletion 17 { 18 readonly Task task; 19 20 internal TaskAwaiter(Task task) 21 { 22 this.task = task; 23 } 24 25 internal static TaskScheduler TaskScheduler 26 { 27 get 28 { 29 if (SynchronizationContext.Current == null) 30 return TaskScheduler.Default; 31 else 32 return TaskScheduler.FromCurrentSynchronizationContext(); 33 } 34 } 35 36 public bool IsCompleted 37 { 38 get { return task.IsCompleted; } 39 } 40 41 public void OnCompleted(Action continuation) 42 { 43 this.task.ContinueWith( 44 delegate (Task task) { 45 continuation(); 46 }, TaskAwaiter.TaskScheduler); 47 } 48 49 public void GetResult() 50 { 51 try 52 { 53 task.Wait(); 54 } 55 catch (AggregateException ex) 56 { 57 throw ex.InnerExceptions[0]; 58 } 59 } 60 } 61 62 public struct TaskAwaiter
: INotifyCompletion 63 { 64 readonly Task
task; 65 66 internal TaskAwaiter(Task
task) 67 { 68 this.task = task; 69 } 70 71 public bool IsCompleted 72 { 73 get { return task.IsCompleted; } 74 } 75 76 public void OnCompleted(Action continuation) 77 { 78 this.task.ContinueWith( 79 delegate (Task
task) { 80 continuation(); 81 }, TaskAwaiter.TaskScheduler); 82 } 83 84 public T GetResult() 85 { 86 try 87 { 88 return task.Result; 89 } 90 catch (AggregateException ex) 91 { 92 throw ex.InnerExceptions[0]; 93 } 94 } 95 } 96 } 97 98 namespace System.Runtime.CompilerServices 99 {100 public interface INotifyCompletion101 {102 void OnCompleted(Action continuation);103 }104 105 public interface ICriticalNotifyCompletion : INotifyCompletion106 {107 [SecurityCritical]108 void UnsafeOnCompleted(Action continuation);109 }110 111 public interface IAsyncStateMachine112 {113 void MoveNext();114 void SetStateMachine(IAsyncStateMachine stateMachine);115 }116 117 public struct AsyncVoidMethodBuilder118 {119 public static AsyncVoidMethodBuilder Create()120 {121 return new AsyncVoidMethodBuilder();122 }123 124 public void SetException(Exception exception)125 {126 throw exception;127 }128 129 public void SetResult()130 {131 }132 133 public void SetStateMachine(IAsyncStateMachine stateMachine)134 {135 // Should not get called as we don't implement the optimization that this method is used for.136 throw new NotImplementedException();137 }138 139 public void Start
(ref TStateMachine stateMachine) where TStateMachine : IAsyncStateMachine140 {141 stateMachine.MoveNext();142 }143 144 public void AwaitOnCompleted
(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : INotifyCompletion where TStateMachine : IAsyncStateMachine145 {146 awaiter.OnCompleted(stateMachine.MoveNext);147 }148 149 public void AwaitUnsafeOnCompleted
(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : ICriticalNotifyCompletion where TStateMachine : IAsyncStateMachine150 {151 awaiter.OnCompleted(stateMachine.MoveNext);152 }153 }154 155 public struct AsyncTaskMethodBuilder156 {157 TaskCompletionSource
tcs;158 159 public Task Task { get { return tcs.Task; } }160 161 public static AsyncTaskMethodBuilder Create()162 {163 AsyncTaskMethodBuilder b;164 b.tcs = new TaskCompletionSource();165 return b;166 }167 168 public void Start
(ref TStateMachine stateMachine) where TStateMachine : IAsyncStateMachine169 {170 stateMachine.MoveNext();171 }172 173 public void SetStateMachine(IAsyncStateMachine stateMachine)174 {175 // Should not get called as we don't implement the optimization that this method is used for.176 throw new NotImplementedException();177 }178 179 public void AwaitOnCompleted
(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : INotifyCompletion where TStateMachine : IAsyncStateMachine180 {181 awaiter.OnCompleted(stateMachine.MoveNext);182 }183 184 public void AwaitUnsafeOnCompleted
(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : ICriticalNotifyCompletion where TStateMachine : IAsyncStateMachine185 {186 awaiter.OnCompleted(stateMachine.MoveNext);187 }188 189 public void SetResult()190 {191 tcs.SetResult(null);192 }193 194 public void SetException(Exception exception)195 {196 tcs.SetException(exception);197 }198 }199 200 public struct AsyncTaskMethodBuilder
201 {202 TaskCompletionSource
tcs;203 204 public Task
Task { get { return tcs.Task; } }205 206 public static AsyncTaskMethodBuilder
Create()207 {208 AsyncTaskMethodBuilder
b;209 b.tcs = new TaskCompletionSource
();210 return b;211 }212 213 public void Start
(ref TStateMachine stateMachine) where TStateMachine : IAsyncStateMachine214 {215 stateMachine.MoveNext();216 }217 218 public void SetStateMachine(IAsyncStateMachine stateMachine)219 {220 // Should not get called as we don't implement the optimization that this method is used for.221 throw new NotImplementedException();222 }223 224 public void AwaitOnCompleted
(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : INotifyCompletion where TStateMachine : IAsyncStateMachine225 {226 awaiter.OnCompleted(stateMachine.MoveNext);227 }228 229 public void AwaitUnsafeOnCompleted
(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : ICriticalNotifyCompletion where TStateMachine : IAsyncStateMachine230 {231 AwaitOnCompleted(ref awaiter, ref stateMachine);232 }233 234 public void SetResult(T result)235 {236 tcs.SetResult(result);237 }238 239 public void SetException(Exception exception)240 {241 tcs.SetException(exception);242 }243 }244 }
hda37210

这段是对 Task 的扩展

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading; 6 using System.Threading.Tasks; 7  8 namespace CefSharp 9 {10    public class TaskEx11     {12         public static Task
FromResult
(T t)13 {14 return Task.Factory.StartNew
(() => t);15 }16 public static Task Run(Action action)17 {18 var tcs = new TaskCompletionSource
();19 new Thread(() => {20 try21 {22 action();23 tcs.SetResult(null);24 }25 catch (Exception ex)26 {27 tcs.SetException(ex);28 }29 })30 { IsBackground = true }.Start();31 return tcs.Task;32 }33 public static Task
Run
(Func
function)34 {35 var tcs = new TaskCompletionSource
();36 new Thread(() =>37 {38 try39 {40 tcs.SetResult(function());41 }42 catch (Exception ex)43 {44 tcs.SetException(ex);45 }46 })47 { IsBackground = true }.Start();48 return tcs.Task;49 }50 public static Task Delay(int milliseconds)51 {52 var tcs = new TaskCompletionSource
();53 var timer = new System.Timers.Timer(milliseconds) { AutoReset = false };54 timer.Elapsed += delegate { timer.Dispose(); tcs.SetResult(null); };55 timer.Start();56 return tcs.Task;57 }58 }59 }
hda37210

把在C#内添加以上代码里, 遇到 Task.Run 的时候,替换成 TaskEx.Run,遇到 Task.Delay 替换为TaskEx.Delay.

还有报 GetTypeInfo 这个错误的地方,删之就Ok了,

祝您顺利!

转载于:https://www.cnblogs.com/had37210/p/8057042.html

你可能感兴趣的文章
Atitit 图像处理30大经典算法attilax总结
查看>>
git 基本操作
查看>>
改变jenkins主目录
查看>>
eclipse 代码补全
查看>>
UVa 10054
查看>>
自定义日期和时间格式字符串
查看>>
读metronic文档学到的几个知识点
查看>>
腾讯CDC谈扁平化设计
查看>>
回溯算法
查看>>
关于oracle数据库的应用
查看>>
继承、实现、依赖、关联、聚合、组合的联系与区别
查看>>
OA办公系统为什么能走到今天?
查看>>
Drupal7 中隐藏某种类型的node 中的字段
查看>>
acdream 1211 Reactor Cooling 【边界网络流量 + 输出流量】
查看>>
HDU 4825 Xor Sum 字典树+位运算
查看>>
Java synchronized
查看>>
FastDFS的配置、部署与API使用解读(3)以流的方式上传文件的客户端代码
查看>>
Team Geek 阅读笔记之 第二章 Building an Awesome Team Culture
查看>>
「文章转载」他译作“维基”的那个网站消失在竹幕后
查看>>
java对象的序列化
查看>>