CefSharp v62 Modification Method of Support.net4.0

  • 2021-10-15 10:23:46
  • OfStack

Tucao 1, the blog park has not been on for a long time, and the account number is gone, so apply for one again.

cesharp v 62 version, the kernel uses the latest Cef 62, support the latest Grid layout. As the official cefsharp uses. net 4.5. 2 development. How to do. I can only use. net 4.0. No way ah, their source code to modify compatible Bai.

A careful analysis of the source code reveals that:

1. net4.5. 2 introduced async/await keyword. In fact, this foreign great god has already put out the active code, so we directly introduced the code into cefsharp project. You can directly use async/await in 4.0;

2. net4.5 extends task api, and we only need the corresponding api. under. net4.0 implementation 1.

3. Inside the source code with a lot of 4.5 only GetTypeInfo extension method error. It returns the type is typeinfo, don't worry about it, GetTypeInfo deleted. Direct Type call can be.

4. To extend the static method of Task, it is necessary to modify the calling mode of static method under 1.

These are the main points. Post the source code below:

This source code is to: async/await support:


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 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 #, when Task. Run is encountered, replace it with TaskEx. Run, and when Task. Delay is encountered, replace it with TaskEx. Delay.

There is also an error in reporting GetTypeInfo. If you delete it, it will be Ok.


Related articles: