diff --git a/CefSharp.Core/Internals/CefBrowserHostWrapper.cpp b/CefSharp.Core/Internals/CefBrowserHostWrapper.cpp index 7151d1ab0..5bf38c7e8 100644 --- a/CefSharp.Core/Internals/CefBrowserHostWrapper.cpp +++ b/CefSharp.Core/Internals/CefBrowserHostWrapper.cpp @@ -109,8 +109,9 @@ Task^ CefBrowserHostWrapper::GetZoomLevelAsync() if (CefCurrentlyOn(TID_UI)) { - TaskCompletionSource^ taskSource = gcnew TaskCompletionSource(); - taskSource->SetResult(GetZoomLevelOnUI()); + auto taskSource = gcnew TaskCompletionSource(); + + CefSharp::Internals::TaskExtensions::TrySetResultAsync(taskSource, GetZoomLevelOnUI()); return taskSource->Task; } return Cef::UIThreadTaskFactory->StartNew(gcnew Func(this, &CefBrowserHostWrapper::GetZoomLevelOnUI)); diff --git a/CefSharp.Core/Internals/CefGetGeolocationCallbackWrapper.h b/CefSharp.Core/Internals/CefGetGeolocationCallbackWrapper.h index 6e748de9c..360b2c0d6 100644 --- a/CefSharp.Core/Internals/CefGetGeolocationCallbackWrapper.h +++ b/CefSharp.Core/Internals/CefGetGeolocationCallbackWrapper.h @@ -31,7 +31,8 @@ namespace CefSharp { if (hasData == false) { - _taskCompletionSource->SetResult(nullptr); + //Set the result on the ThreadPool so the Task continuation is not run on the CEF UI Thread + TaskExtensions::TrySetResultAsync(_taskCompletionSource, nullptr); } _taskCompletionSource = nullptr; } @@ -50,7 +51,9 @@ namespace CefSharp p->Speed = position.speed; p->Timestamp = ConvertCefTimeToDateTime(position.timestamp); - _taskCompletionSource->SetResult(p); + //Set the result on the ThreadPool so the Task continuation is not run on the CEF UI Thread + TaskExtensions::TrySetResultAsync(_taskCompletionSource, p); + hasData = true; }; diff --git a/CefSharp.Core/Internals/ClientAdapter.cpp b/CefSharp.Core/Internals/ClientAdapter.cpp index 6467178b0..39b63fb93 100644 --- a/CefSharp.Core/Internals/ClientAdapter.cpp +++ b/CefSharp.Core/Internals/ClientAdapter.cpp @@ -1112,7 +1112,7 @@ namespace CefSharp response->Message = StringUtils::ToClr(argList->GetString(2)); } - pendingTask->SetResult(response); + CefSharp::Internals::TaskExtensions::TrySetResultAsync(pendingTask, response); } handled = true; diff --git a/CefSharp.Core/Internals/CookieManager.cpp b/CefSharp.Core/Internals/CookieManager.cpp index 9c0654f4a..7068939ea 100644 --- a/CefSharp.Core/Internals/CookieManager.cpp +++ b/CefSharp.Core/Internals/CookieManager.cpp @@ -31,7 +31,7 @@ namespace CefSharp if (CefCurrentlyOn(TID_IO)) { auto source = gcnew TaskCompletionSource(); - source->TrySetResult(cookieInvoker->DeleteCookies()); + TaskExtensions::TrySetResultAsync(source, cookieInvoker->DeleteCookies()); return source->Task; } @@ -47,7 +47,7 @@ namespace CefSharp if (CefCurrentlyOn(TID_IO)) { auto source = gcnew TaskCompletionSource(); - source->TrySetResult(cookieInvoker->SetCookie()); + TaskExtensions::TrySetResultAsync(source, cookieInvoker->SetCookie()); return source->Task; } diff --git a/CefSharp.Core/Internals/PluginVisitor.h b/CefSharp.Core/Internals/PluginVisitor.h index a768cca82..d7012a407 100644 --- a/CefSharp.Core/Internals/PluginVisitor.h +++ b/CefSharp.Core/Internals/PluginVisitor.h @@ -7,6 +7,7 @@ #include "Stdafx.h" #include "include/cef_web_plugin.h" +using namespace CefSharp::Internals; using namespace System::Threading::Tasks; namespace CefSharp @@ -29,7 +30,8 @@ namespace CefSharp //In this case Visit was likely never called, so we set result to complete the task if (_list->Count == 0) { - _taskCompletionSource->SetResult(_list); + //Set the result on the ThreadPool so the Task continuation is not run on the CEF UI Thread + CefSharp::Internals::TaskExtensions::TrySetResultAsync^>(_taskCompletionSource, _list); } _list = nullptr; @@ -53,7 +55,8 @@ namespace CefSharp if(count == (total - 1)) { - _taskCompletionSource->SetResult(_list); + //Set the result on the ThreadPool so the Task continuation is not run on the CEF UI Thread + CefSharp::Internals::TaskExtensions::TrySetResultAsync^>(_taskCompletionSource, _list); } return true; diff --git a/CefSharp.OffScreen.Example/Program.cs b/CefSharp.OffScreen.Example/Program.cs index 8bd88f86b..e8d6db089 100644 --- a/CefSharp.OffScreen.Example/Program.cs +++ b/CefSharp.OffScreen.Example/Program.cs @@ -6,8 +6,10 @@ using System; using System.Diagnostics; using System.Drawing; using System.IO; +using System.Linq; using System.Threading.Tasks; using CefSharp.Example; +using CefSharp.Internals; namespace CefSharp.OffScreen.Example { @@ -63,7 +65,18 @@ namespace CefSharp.OffScreen.Example } await LoadPageAsync(browser); - var preferences = requestContext.GetAllPreferences(true); + //Check preferences on the CEF UI Thread + await Cef.UIThreadTaskFactory.StartNew(delegate + { + var preferences = requestContext.GetAllPreferences(true); + + //Check do not track status + var doNotTrack = (bool)preferences["enable_do_not_track"]; + + Debug.WriteLine("DoNotTrack:" + doNotTrack); + }); + + var onUi = Cef.CurrentlyOnThread(CefThreadIds.TID_UI); // For Google.com pre-pupulate the search text box await browser.EvaluateScriptAsync("document.getElementById('lst-ib').value = 'CefSharp Was Here!'"); @@ -100,7 +113,7 @@ namespace CefSharp.OffScreen.Example if (!args.IsLoading) { browser.LoadingStateChanged -= handler; - tcs.TrySetResult(true); + tcs.TrySetResultAsync(true); } }; diff --git a/CefSharp.OffScreen/ChromiumWebBrowser.cs b/CefSharp.OffScreen/ChromiumWebBrowser.cs index 140728840..7987bba8b 100644 --- a/CefSharp.OffScreen/ChromiumWebBrowser.cs +++ b/CefSharp.OffScreen/ChromiumWebBrowser.cs @@ -264,14 +264,14 @@ namespace CefSharp.OffScreen // Chromium has rendered. Tell the task about it. NewScreenshot -= newScreenshot; - completionSource.SetResult(ScreenshotOrNull()); + completionSource.TrySetResultAsync(ScreenshotOrNull()); }; NewScreenshot += newScreenshot; } else { - completionSource.SetResult(screenshot); + completionSource.TrySetResultAsync(screenshot); } return completionSource.Task; diff --git a/CefSharp/Internals/TaskExtensions.cs b/CefSharp/Internals/TaskExtensions.cs index 75909f354..2d43880b0 100644 --- a/CefSharp/Internals/TaskExtensions.cs +++ b/CefSharp/Internals/TaskExtensions.cs @@ -87,5 +87,17 @@ namespace CefSharp.Internals return taskCompletionSource; } + + /// + /// Set the TaskCompletionSource in an async fashion. This prevents the Task Continuation being executed sync on the same thread + /// This is required otherwise contintinuations will happen on CEF UI threads + /// + /// Generic param + /// tcs + /// result + public static void TrySetResultAsync(this TaskCompletionSource taskCompletionSource, TResult result) + { + Task.Factory.StartNew(delegate { taskCompletionSource.TrySetResult(result); }, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default); + } } } diff --git a/CefSharp/TaskCompletionHandler.cs b/CefSharp/TaskCompletionHandler.cs index 79cfaa02a..8d3e0c5de 100644 --- a/CefSharp/TaskCompletionHandler.cs +++ b/CefSharp/TaskCompletionHandler.cs @@ -3,6 +3,7 @@ // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. using System.Threading.Tasks; +using CefSharp.Internals; namespace CefSharp { @@ -17,7 +18,7 @@ namespace CefSharp public void OnComplete() { - taskCompletionSource.SetResult(true); + taskCompletionSource.TrySetResultAsync(true); } public Task Task diff --git a/CefSharp/TaskPrintToPdf.cs b/CefSharp/TaskPrintToPdf.cs index e31a456ca..c74931df7 100644 --- a/CefSharp/TaskPrintToPdf.cs +++ b/CefSharp/TaskPrintToPdf.cs @@ -3,6 +3,7 @@ // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. using System.Threading.Tasks; +using CefSharp.Internals; namespace CefSharp { @@ -17,7 +18,7 @@ namespace CefSharp public void OnPdfPrintFinished(string path, bool ok) { - taskCompletionSource.TrySetResult(ok); + taskCompletionSource.TrySetResultAsync(ok); } } } diff --git a/CefSharp/TaskResolveCallbackHandler.cs b/CefSharp/TaskResolveCallbackHandler.cs index 30426489f..f5f729bfe 100644 --- a/CefSharp/TaskResolveCallbackHandler.cs +++ b/CefSharp/TaskResolveCallbackHandler.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Threading.Tasks; +using CefSharp.Internals; namespace CefSharp { @@ -18,7 +19,7 @@ namespace CefSharp public void OnResolveCompleted(CefErrorCode result, IList resolvedIpAddresses) { - taskCompletionSource.SetResult(new ResolveCallbackResult(result, resolvedIpAddresses)); + taskCompletionSource.TrySetResultAsync(new ResolveCallbackResult(result, resolvedIpAddresses)); } public Task Task diff --git a/CefSharp/TaskStringVisitor.cs b/CefSharp/TaskStringVisitor.cs index d20bb35a9..53f4a3dc8 100644 --- a/CefSharp/TaskStringVisitor.cs +++ b/CefSharp/TaskStringVisitor.cs @@ -3,6 +3,7 @@ // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. using System.Threading.Tasks; +using CefSharp.Internals; namespace CefSharp { @@ -28,7 +29,7 @@ namespace CefSharp /// string (result of async execution) public void Visit(string str) { - taskCompletionSource.SetResult(str); + taskCompletionSource.TrySetResultAsync(str); } ///