Cefsharp v62 modification method (supports. Net4.0)

Time:2020-7-14

Make complaints about it. Blog has not been on for a long time, and the account number has been lost.

Cesharp v62 version, the kernel uses the latest CEF 62, supports the latest grid layout. Because the official cefsharp is developed with. Net4.5.2, what to do? I can only use. Net4.0. I can’t help it. I can’t help but modify the source code by myself

Careful analysis of the source code shows that:

Net4.5.2 introduces the async / await keyword. In fact, the source code of foreign gods has been released. We can directly introduce the code into cefsharp project. We can use async / await directly in 4.0;

Net4.5 extends the task API. We only need to implement the corresponding API in. Net4.0

3. The source code uses a lot of gettypeinfo extension methods which are only available in 4.5. The returned type is TypeInfo. Don’t worry about it. Delete gettypeinfo. Call type directly

4. For the extension of task static method, we need to modify the calling method of static method

The above is the main point

The source code of this section is to support async / await


namespace System.Threading.Tasks
{
 public static class TaskEx
 {
 public static TaskAwaiter GetAwaiter(this Task task)
 {
  return new TaskAwaiter(task);
 }
 public static TaskAwaiter<T> GetAwaiter<T>(this Task<T> task)
 {
  return new TaskAwaiter<T>(task);
 }
 }
 public struct TaskAwaiter : INotifyCompletion
 {
 readonly Task task;
 internal TaskAwaiter(Task task)
 {
  this.task = task;
 }
 internal static TaskScheduler TaskScheduler
 {
  get
  {
  if (SynchronizationContext.Current == null)
   return TaskScheduler.Default;
  else
   return TaskScheduler.FromCurrentSynchronizationContext();
  }
 }
 public bool IsCompleted
 {
  get { return task.IsCompleted; }
 }
 public void OnCompleted(Action continuation)
 {
  this.task.ContinueWith(
  delegate (Task task) {
   continuation();
  }, TaskAwaiter.TaskScheduler);
 }
 public void GetResult()
 {
  try
  {
  task.Wait();
  }
  catch (AggregateException ex)
  {
  throw ex.InnerExceptions[0];
  }
 }
 }
 public struct TaskAwaiter<T> : INotifyCompletion
 {
 readonly Task<T> task;
 internal TaskAwaiter(Task<T> task)
 {
  this.task = task;
 }
 public bool IsCompleted
 {
  get { return task.IsCompleted; }
 }
 public void OnCompleted(Action continuation)
 {
  this.task.ContinueWith(
  delegate (Task<T> task) {
   continuation();
  }, TaskAwaiter.TaskScheduler);
 }
 public T GetResult()
 {
  try
  {
  return task.Result;
  }
  catch (AggregateException ex)
  {
  throw ex.InnerExceptions[0];
  }
 }
 }
}
namespace System.Runtime.CompilerServices
{
 public interface INotifyCompletion
 {
 void OnCompleted(Action continuation);
 }
 public interface ICriticalNotifyCompletion : INotifyCompletion
 {
 [SecurityCritical]
 void UnsafeOnCompleted(Action continuation);
 }
 public interface IAsyncStateMachine
 {
 void MoveNext();
 void SetStateMachine(IAsyncStateMachine stateMachine);
 }
 public struct AsyncVoidMethodBuilder
 {
 public static AsyncVoidMethodBuilder Create()
 {
  return new AsyncVoidMethodBuilder();
 }
 public void SetException(Exception exception)
 {
  throw exception;
 }
 public void SetResult()
 {
 }
 public void SetStateMachine(IAsyncStateMachine stateMachine)
 {
  // Should not get called as we don't implement the optimization that this method is used for.
  throw new NotImplementedException();
 }
 public void Start<TStateMachine>(ref TStateMachine stateMachine) where TStateMachine : IAsyncStateMachine
 {
  stateMachine.MoveNext();
 }
 public void AwaitOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : INotifyCompletion where TStateMachine : IAsyncStateMachine
 {
  awaiter.OnCompleted(stateMachine.MoveNext);
 }
 public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : ICriticalNotifyCompletion where TStateMachine : IAsyncStateMachine
 {
  awaiter.OnCompleted(stateMachine.MoveNext);
 }
 }
 public struct AsyncTaskMethodBuilder
 {
 TaskCompletionSource<object> tcs;
 public Task Task { get { return tcs.Task; } }
 public static AsyncTaskMethodBuilder Create()
 {
  AsyncTaskMethodBuilder b;
  b.tcs = new TaskCompletionSource<object>();
  return b;
 }
 public void Start<TStateMachine>(ref TStateMachine stateMachine) where TStateMachine : IAsyncStateMachine
 {
  stateMachine.MoveNext();
 }
 public void SetStateMachine(IAsyncStateMachine stateMachine)
 {
  // Should not get called as we don't implement the optimization that this method is used for.
  throw new NotImplementedException();
 }
 public void AwaitOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : INotifyCompletion where TStateMachine : IAsyncStateMachine
 {
  awaiter.OnCompleted(stateMachine.MoveNext);
 }
 public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : ICriticalNotifyCompletion where TStateMachine : IAsyncStateMachine
 {
  awaiter.OnCompleted(stateMachine.MoveNext);
 }
 public void SetResult()
 {
  tcs.SetResult(null);
 }
 public void SetException(Exception exception)
 {
  tcs.SetException(exception);
 }
 }
 public struct AsyncTaskMethodBuilder<T>
 {
 TaskCompletionSource<T> tcs;
 public Task<T> Task { get { return tcs.Task; } }
 public static AsyncTaskMethodBuilder<T> Create()
 {
  AsyncTaskMethodBuilder<T> b;
  b.tcs = new TaskCompletionSource<T>();
  return b;
 }
 public void Start<TStateMachine>(ref TStateMachine stateMachine) where TStateMachine : IAsyncStateMachine
 {
  stateMachine.MoveNext();
 }
 public void SetStateMachine(IAsyncStateMachine stateMachine)
 {
  // Should not get called as we don't implement the optimization that this method is used for.
  throw new NotImplementedException();
 }
 public void AwaitOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : INotifyCompletion where TStateMachine : IAsyncStateMachine
 {
  awaiter.OnCompleted(stateMachine.MoveNext);
 }
 public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : ICriticalNotifyCompletion where TStateMachine : IAsyncStateMachine
 {
  AwaitOnCompleted(ref awaiter, ref stateMachine);
 }
 public void SetResult(T result)
 {
  tcs.SetResult(result);
 }
 public void SetException(Exception exception)
 {
  tcs.SetException(exception);
 }
 }
}

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

Add the above code in C ා, encounter Task.Run Replace with TaskEx.Run , encountered Task.Delay Replace with TaskEx.Delay .

There is also an error in gettypeinfo. Delete it to OK.

The above revision method of cefsharp v62 (supporting. Net4.0) is the whole content shared by Xiaobian. I hope to give you a reference, and I hope you can support developeppaer more.