CefSharp v62 modification, support.net4.0

After spitting, the blog Garden has not been on for a long time, and the account has been lost. Apply again.

cesharp v62, with the latest Cef 62 in the kernel, supports the latest Grid layout. Since the official cefsharp was developed with.net4.5.2, what to do. I can only use.net4.0. No way, I can modify the compatibility with the source code myself.

Careful analysis of source discovery:

1.net4.5.2 introduced the async/await keyword. In fact, the foreign gods have source code released, we can directly introduce the code to the cefsharp project. You can use async/await directly in 4.0;

2.net4.5 extends the task api, so we just need to implement it in.net4.0.

3. There are many 4.5 GetTypeInfo extension method errors in the source code. The type it returns is typeinfo, regardless of it, delete GetTypeInfo. A direct Type call is OK.

4. Extensions to Task static methods require modifications to the way static methods are invoked.

That's the point. Paste the source code below:

This source code is for: async/await support:

  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 }
hda37210

This is an extension of 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 }
hda37210

Add the above code in C#, replace TaskEx.Run when you encounter Task.Run, and replace Task.Delay with TaskEx.Delay when you encounter Task.

There is also an error in GetTypeInfo, delete it and Ok.

Wishing you all the best!

Tags: C#

Posted on Fri, 08 May 2020 13:24:04 -0400 by harishkumar09