我特林創建一個JavaScript對象將其傳遞給函數回調像以下:C++中的自定義線程創建CEF對象
void MyClass::ThreadTaskOnSuccess(CefRefPtr<CefV8Value> callback, CefRefPtr<CefV8Context> callbackCtxt)
{
if (!CefCurrentlyOn(TID_UI))
{
// switch to UI thread
CefPostTask(TID_UI, NewCefRunnableMethod(this, &NewDownloadObject::CreateTempDownloadOnSuccess, callback, callbackCtxt));
return;
}
// String creation works perfect!
// CefRefPtr<CefV8Value> executionResult = CefV8Value::CreateString("test");
// "Access violation" will be thrown
CefRefPtr<CefV8Value> executionResult = CefV8Value::CreateObject(NULL);
executionResult->SetValue("size", CefV8Value::CreateInt(123), V8_PROPERTY_ATTRIBUTE_NONE);
executionResult->SetValue("fileName", CefV8Value::CreateString("some name of file"), V8_PROPERTY_ATTRIBUTE_NONE);
CefV8ValueList args;
args.push_back(executionResult);
CefRefPtr<CefV8Value> retval;
CefRefPtr<CefV8Exception> exception;
if (callback->ExecuteFunctionWithContext(callbackCtxt, callbackCtxt->GetGlobal(), args, retval, exception, false))
{
if (exception.get())
{
throw CFdmException(exception->GetMessage().c_str());
}
else
{
// Execution succeeded.
}
}
}
但CefV8Value ::的CreateObject(NULL)始終返回null結果。我想這是因爲代碼是運行在自定義線程,因爲一些任務必須在特殊線程中執行。
我對不對?以及如何切換到cef線程與V8引擎一起工作並與其同步?
我錯了嗎?爲什麼V8創建一個空對象?
更新
我加入UI線程切換。之後,我總是在cef_v8value_create_object中出現「Access violation reading location」異常,例如the Hzmy's quiestion。
會驚喜我,如果V8是線程安全的(當然,一個單一實例),或者甚至線程感知。你嘗試過只是添加鎖定? – sehe
鎖定並沒有幫助,因爲v8只是創建一個null對象:( – JohanTG