We have created a DLL that uses EO.WebBrowser internally and renders some html via the browser. The DLL method than returns the rendered html to the user. DLL has also a UserControl that shows an html page to the user.
This DLL will be implemented in multiple environments including a Desktop Application and Web pages.
When the users visit the web page, they fill out a form and send it to web server. Webserver processes the form and calls the DLL method with the parameters, and gets the html output and shows to the user.
Since EO.WebBrowser needs a WebView and ThreadRunner object; what is the best way to use WebView and ThreadRunner objects in this scenario?
Currently, we have created a Start(), Stop() and RenderHTML() methods for the dll. And we call all of these methods for each form submit. But the IIS worker sometimes uses up to 600MB of memory even though there are no requests. Then it suddenly drops down to 200MB and seconds later it goes back up to 600MB (no requests still). Is it okay to create webview and threadrunner for each form submit? Is there any other method that uses same thread runner and webview object (without disposing and creating again) for different form submits in the web server?
Here are methods and how the DLL is used:
DLL:
Code: C#
public class MyDLL
{
ThreadRunner threadRunner;
WebView webView;
bool initDone = false;
public void Start()
{
EO.WebBrowser.Runtime.AddLicense("***");
if (threadRunner == null) threadRunner = new ThreadRunner();
if (webView == null) webView = threadRunner.CreateWebView();
var page = new Uri(string.Format("file:///{0}/html_resources/index.html", Utilities.AppLocation)).ToString();
threadRunner.Send(() => { webView.LoadUrlAndWait(page); });
initDone = true;
}
public void Stop()
{
if (webView != null)
{
webView.Destroy();
webView.Dispose();
}
if (threadRunner != null)
{
threadRunner.Stop();
threadRunner.Dispose();
}
initDone = false;
}
public string RenderHTML(string input)
{
if (!initDone) return "Error: DLL not initalized!";
var renderedHTML = "";
var scriptToExecute = string.Format("my_js_function(`{0}`, {1})", input);
threadRunner.Send(() => { renderedHTML = (string)webView.EvalScript(scriptToExecute); });
return renderedHTML;
}
}
Web Method (process.ashx)
Code: C#
public void ProcessRequest(HttpContext context)
{
MyDLL dll = new MyDLL();
dll.Start();
string result = dll.RenderHTML(form_input);
dll.Stop();
context.Response.ContentEncoding = System.Text.Encoding.UTF8;
context.Response.Write(result);
}