Awesomium Error: The calling thread cannnot access this object because a different thread owns it.
Der Fehler kommt auf, wenn man mit dem Awesomium Chrome Browser Control zwei mal auf den document Code zugreift.
Unter einer reinen Windows Forms Anwendung kommt der Fehler nicht.
Betrifft: Awesomium webBrowser Control unter Windows Forms 32 in einer Microsoft Office Anwendung.
Fehlermeldung:
An exception of type “Awesomium.Core.AweInvalidOperationException” occurred in Awesomium.Core.dll but was not handled in user code.
Addintional information: The calling thread cannnot access this object because a different thread owns it.
|
Fehlermeldung in Visual Studio zur Laufzeit:
Der Fehler geschieht, wenn man zwei mal hintereinander in einer Methode auf ein webControl von Awesomium zugreift.
Lösung:
Unter Windows Forms 32 muss man mit Control.Invoke( new Action() ) einen neuen Thread öffnen.
Das heißt, man muss den alten Code einfach innerhalb der neuen invoke action klammer einfügen
//-------------------< set_Translation() >-------------------
//*textarea id=result_box
//WPF: System.Windows.Threading.Dispatcher
//Windows Forms: Invoke direkt vom Control aus
this.Invoke(new Action(() =>
{
string sResult = webControl1.ExecuteJavascriptWithResult("document.getElementById('result_box').textContent");
MessageBox.Show(sResult);
})
);
|
Alter Code: Ohne Invoke
//-------------------< set_Translation() >-------------------
//*textarea id=result_box
string sResult = webControl1.ExecuteJavascriptWithResult("document.getElementById('result_box').textContent");
MessageBox.Show(sResult);
|
Gelöst:
C# Code zur Korrektur
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Security.Permissions; //comvisible
using System.Runtime.InteropServices; //comvisible
using security =System.Security;
using Microsoft.Win32; //Registry
using System.Windows.Threading;
namespace Word_Add_In_Microsoft_Programmierer
{
public partial class frmTranslate : Form
{
//< locals >
mshtml.HTMLDocument doc;
//</ locals >
#region ====< Form >====
public frmTranslate()
{
InitializeComponent();
}
#endregion ====</ Form >====
#region =====< Buttons >====
private void btnOpen_Click(object sender, EventArgs e)
{
//error: ctlBrowser.Navigate("http://www.Google.de");
//*opens website
webControl1.Source = new Uri("http://translate.google.com?hl=en#de/en/");
}
private async void btnTranslate_Click(object sender, EventArgs e)
{
// -------------------< btnTranslate_Click() > -------------------
set_Translation();
//----< wait 1 second >----
await Task.Delay(1000);
//----</ wait 1 second >----
//----< wait doc.complete >----
DateTime dtStart = DateTime.Now;
while (webControl1.IsDocumentReady != true )
{
if ((DateTime.Now - dtStart).Seconds > 5) break;
await Task.Delay(1);
}
//----</ wait doc.complete >----
get_Translation();
//-------------------</ btnTranslate_Click() >-------------------
}
#endregion =====</ Buttons >====
#region =====< Translation >====
private void set_Translation()
{
//-------------------< set_Translation() >-------------------
//*textarea id=source
webControl1.ExecuteJavascript("document.getElementById('source').value='Mein Name ist Raimund'");
//-------------------</ get_Results() >-------------------
}
private void get_Translation()
{
//-------------------< set_Translation() >-------------------
//*textarea id=result_box
//WPF: System.Windows.Threading.Dispatcher
//Windows Forms: Invoke direkt vom Control aus
this.Invoke(new Action(() =>
{
string sResult = webControl1.ExecuteJavascriptWithResult("document.getElementById('result_box').textContent");
MessageBox.Show(sResult);
})
);
//-------------------</ get_Results() >-------------------
}
#endregion =====< Translation >====
}
}
|