当前的SynchronizationContext可能不会用作TaskScheduler。来自CEFsharp EvaluateScriptAsync

时间:2015-12-11 03:49:55

标签: c# cefsharp

我试图通过CEFSharp在CEF浏览器中异步执行一些JS代码。 我的代码看起来像;

debugForm = new CEFdebugger();
debugForm.browser.LoadingStateChanged += new EventHandler<CefSharp.LoadingStateChangedEventArgs>(webBrowser_LoadingStateChanged);
...
debugForm.browser.Load("local://wwwpub/index.html");
debugForm.Show();
...

void webBrowser_LoadingStateChanged(object sender, CefSharp.LoadingStateChangedEventArgs e)
{
    if (!e.IsLoading)
    {
            var task = debugForm.browser.EvaluateScriptAsync("1+1");
            task.ContinueWith(response =>
            {
...
            }, TaskScheduler.FromCurrentSynchronizationContext());
        }
    }

}

该行;

task.ContinueWith(response =>

引发

System.InvalidOperationException was unhandled
HResult=-2146233079
Message=The current SynchronizationContext may not be used as a TaskScheduler.
Source=mscorlib
StackTrace:
   at System.Threading.Tasks.SynchronizationContextTaskScheduler..ctor()
   at System.Threading.Tasks.TaskScheduler.FromCurrentSynchronizationContext()
   at KeyHandler.Interpreter.webBrowser_LoadingStateChanged(Object sender, LoadingStateChangedEventArgs e) in D:\development\Cubist\software\IoTkeys\IoTkeys\KeyHandler\Interpreter.cs:line 120
   at CefSharp.WinForms.ChromiumWebBrowser.CefSharp.Internals.IWebBrowserInternal.SetLoadingStateChange(LoadingStateChangedEventArgs args)
   at CefSharp.Internals.ClientAdapter.OnLoadingStateChange(ClientAdapter* , CefRefPtr<CefBrowser>* browser, Boolean isLoading, Boolean canGoBack, Boolean canGoForward)

如何解决此问题?

1 个答案:

答案 0 :(得分:2)

通过替换

解决了问题
task.ContinueWith(response =>
{
    ...
}, TaskScheduler.FromCurrentSynchronizationContext());

用这个

task.ContinueWith(response =>
{
    ...
});