Welcome Guest Search | Active Topics | Sign In | Register

Best Practice about using WebBrowser in DLL Options
MMT
Posted: Wednesday, June 8, 2016 2:04:36 AM
Rank: Newbie
Groups: Member

Joined: 6/8/2016
Posts: 4
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);
        }
eo_support
Posted: Wednesday, June 8, 2016 10:51:10 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,229
Hi,

There is no need for you to create and destroy a new ThreadRunner and WebView every time. Both ThreadRunner and WebView can be reused. However if you do need reuse them, your user might have to wait longer ---- for example, if another user is using the ThreadRunner (when the Send method is waiting), then the next user will have to wait (the next Send will block until the first Send is done).

Thanks!
MMT
Posted: Wednesday, June 8, 2016 10:53:29 AM
Rank: Newbie
Groups: Member

Joined: 6/8/2016
Posts: 4
Thanks for the quick reply. How can i re-use these objects in a web environment? Since every web request is independent of each other.

Also, do you have any comments about the memory issue? Why is it suddenly increasing from 200MB to 600MB without any method calls?
eo_support
Posted: Wednesday, June 8, 2016 10:58:23 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,229
You just keep them as global variables.

The memory usage are not something we have precise control over with, especially when our code is not even called. So we won't be able to comment on that.

Thanks!
MMT
Posted: Wednesday, June 8, 2016 11:00:33 AM
Rank: Newbie
Groups: Member

Joined: 6/8/2016
Posts: 4
Actually, memory raises after webview and threadrunner are initialized. But it does not decrease after they are destroyed. I say "no method calls" because there are no new web requests (development server is local) but once webview and threadrunner are called, memory increases and so on.

But i will try the global scenario and if the memory issue does not get resolved, i will get in touch again.

Thanks for the help.


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.