吐槽一下,博客园久了没有上,账号没了,重新申请一个.
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<T> GetAwaiter<T>(this Task<T> task) 11 { 12 return new TaskAwaiter<T>(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<T> : INotifyCompletion 63 { 64 readonly Task<T> task; 65 66 internal TaskAwaiter(Task<T> 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<T> 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 INotifyCompletion 101 { 102 void OnCompleted(Action continuation); 103 } 104 105 public interface ICriticalNotifyCompletion : INotifyCompletion 106 { 107 [SecurityCritical] 108 void UnsafeOnCompleted(Action continuation); 109 } 110 111 public interface IAsyncStateMachine 112 { 113 void MoveNext(); 114 void SetStateMachine(IAsyncStateMachine stateMachine); 115 } 116 117 public struct AsyncVoidMethodBuilder 118 { 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<TStateMachine>(ref TStateMachine stateMachine) where TStateMachine : IAsyncStateMachine 140 { 141 stateMachine.MoveNext(); 142 } 143 144 public void AwaitOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : INotifyCompletion where TStateMachine : IAsyncStateMachine 145 { 146 awaiter.OnCompleted(stateMachine.MoveNext); 147 } 148 149 public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : ICriticalNotifyCompletion where TStateMachine : IAsyncStateMachine 150 { 151 awaiter.OnCompleted(stateMachine.MoveNext); 152 } 153 } 154 155 public struct AsyncTaskMethodBuilder 156 { 157 TaskCompletionSource<object> 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<object>(); 165 return b; 166 } 167 168 public void Start<TStateMachine>(ref TStateMachine stateMachine) where TStateMachine : IAsyncStateMachine 169 { 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<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : INotifyCompletion where TStateMachine : IAsyncStateMachine 180 { 181 awaiter.OnCompleted(stateMachine.MoveNext); 182 } 183 184 public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : ICriticalNotifyCompletion where TStateMachine : IAsyncStateMachine 185 { 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<T> 201 { 202 TaskCompletionSource<T> tcs; 203 204 public Task<T> Task { get { return tcs.Task; } } 205 206 public static AsyncTaskMethodBuilder<T> Create() 207 { 208 AsyncTaskMethodBuilder<T> b; 209 b.tcs = new TaskCompletionSource<T>(); 210 return b; 211 } 212 213 public void Start<TStateMachine>(ref TStateMachine stateMachine) where TStateMachine : IAsyncStateMachine 214 { 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<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : INotifyCompletion where TStateMachine : IAsyncStateMachine 225 { 226 awaiter.OnCompleted(stateMachine.MoveNext); 227 } 228 229 public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : ICriticalNotifyCompletion where TStateMachine : IAsyncStateMachine 230 { 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 }
这段是对 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 TaskEx 11 { 12 public static Task<T> FromResult<T>(T t) 13 { 14 return Task.Factory.StartNew<T>(() => t); 15 } 16 public static Task Run(Action action) 17 { 18 var tcs = new TaskCompletionSource<object>(); 19 new Thread(() => { 20 try 21 { 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<TResult> Run<TResult>(Func<TResult> function) 34 { 35 var tcs = new TaskCompletionSource<TResult>(); 36 new Thread(() => 37 { 38 try 39 { 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<object>(); 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 }
把在C#内添加以上代码里, 遇到 Task.Run 的时候,替换成 TaskEx.Run,遇到 Task.Delay 替换为TaskEx.Delay.
还有报 GetTypeInfo 这个错误的地方,删之就Ok了,
祝您顺利!