I’m writing an application that loads some javascript into a WebBrowser object. To prevent this from getting loaded and the lag associated with it, I am initializing it on the main UI thread when my app loads and keeping a static reference to it.
The problem comes when I have other threads that want to use it. Thus, I am trying to use the Monitor class to help me out.
On the main thread I have
public static readonly WebBrowser webBrowser = new WebBrowser();
private static readonly object syncObject = new object();
when the other threads call it, I have the following function.
public static string GetDataFromJs(string key)
{
string result = string.Empty;
if (isInitialized == true)
{
try
{
webBrowser.Dispatcher.BeginInvoke(() =>
{
result = (string)webBrowser.InvokeScript("getData", key);
//TODO: pulse monitor object.
});
//TODO: wait for monitor object to get pulsed.
return result;
}
catch(Exception ex)
{
return string.Empty;
}
}
return string.Empty;
}
Can anyone help me figure out what I need to do from here? I’ve tried Monitor.Wait/Enter and Monitor.Pulse in thier respective comment areas, but I keep getting Syncronization exceptions.
Thanks!
public static string GetDataFromJs(string key)
{
string result = string.Empty;
}
Make sure this is right 🙂