I've been working on a C# WinForm project that has a part in which when a user keys up "enter" button, a BackgroundWorker will set a new URI in the Source property of the Awesomium Instance. However, I'm encountering an AweInvalidOperationException ("The calling thread cannot access this object because a different thread owns it.").
Here's a sample code of a part of my work:
private void txt_To_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
backgroundWorker.RunWorkerAsync();
}
}
private void backgroundWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
//Get values from an XML file and everything (Not included here anymore for it's too long)
awesomiumInstance.Source = new Uri("http://maps.google.com?saddr=" +
txt_From.Text.Replace(" ", "+") + "&daddr=" + txt_To.Text.Replace(" ", "+") + flag +
"&view=map&hl=en&doflg=ptk");
}
The AweInvalidOperationException happens at line 12
awesomiumInstance.Source = new Uri("http://maps.google.com?saddr=" +
txt_From.Text.Replace(" ", "+") + "&daddr=" +
txt_To.Text.Replace(" ", "+") + flag +
"&view=map&hl=en&doflg=ptk");
Here's the screenshot of the exception:
What could be the fix/solution to this exception?