CefSharp v62修改方法(支持.net4.0)
程序员文章站
2022-04-29 12:38:40
吐槽一下,博客园久了没有上,账号没了,重新申请一个.
cesharp v62版本,内核采用最新的cef 62,支持最新的grid布局. 由于官方的cefsharp 采用....
吐槽一下,博客园久了没有上,账号没了,重新申请一个.
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的支持:
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); } } }
这段是对 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; } } }
把在c#内添加以上代码里, 遇到 task.run 的时候,替换成 taskex.run,遇到 task.delay 替换为taskex.delay.
还有报 gettypeinfo 这个错误的地方,删之就ok了。
以上这篇cefsharp v62修改方法(支持.net4.0)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
上一篇: WPF水珠效果按钮组的实现教程
下一篇: vue 页面加载进度条组件实例