Welcome Guest Search | Active Topics | Sign In | Register

Either the ThreadRunner object must be explicitly destroyed or all WebViews associated to the Thread Options
Omri Suissa
Posted: Monday, March 1, 2021 11:09:41 AM
Rank: Member
Groups: Member

Joined: 2/18/2020
Posts: 24
Hi,
I get:
"Either the ThreadRunner object must be explicitly destroyed or all WebViews associated to the ThreadRunner must have been destroyed before exiting the application or shutting down the AppDomain"

Stack trace:
EO.Base.Runtime.sqkq(Object kkt, Exception kku, Boolean kkv)
EO.Base.ThreadRunnerBase.OnException(Exception ex, Boolean exiting)
EO.Base.ThreadRunnerBase.bwrm(Boolean knf)
EO.Base.ThreadRunnerBase.bwrm()
EO.Internal.dnvo.bwrm()
System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
System.Threading.ThreadHelper.ThreadStart()

However, my code disposes everything (as far as I understand):
Code: C#
var threadRunner = new ThreadRunner();
            var webView = threadRunner.CreateWebView();

            try
            {
                webView.CertificateError += (s, e) => { e.Continue(); };
                webView.Resize(1920, 1000);

                threadRunner.Send(() =>
                {
                    try
                    {

                        try
                        {
                            foreach (var item in myCookies.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries))
                            {
                                try
                                {
                                    var nameAndValue = item.Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries);
                                    if (nameAndValue.Length > 1)
                                    {
                                        webView.Engine.CookieManager.SetCookie(new EO_WebEngine.Cookie(nameAndValue[0], nameAndValue[1]));
                                    }
                                }
                                catch { }
                            }
                        }
                        catch { }
                        
                        var navigation = webView.LoadUrl(myUrl);
                        navigation.WaitOne(10 * 1000);

                        if (navigation.HttpStatusCode == 200)
                        {
                            Thread.Sleep(3000);

                            var html = webView.EvalScript("document.documentElement.outerHTML", false).ToString();

                            if (!string.IsNullOrWhiteSpace(html))
                            {
                                var url = webView.EvalScript("document.location.href", false).ToString();

                                if (!string.IsNullOrWhiteSpace(url))
                                {
                                    // some logic using the page's HTML and URL...
                                }
                            }
                        }

                        try
                        {
                            webView.Dispose();
                        }
                        catch { }
                    }
                    catch { }

                    try
                    {
                        threadRunner.Dispose();
                    }
                    catch { }

                    try
                    {
                        GC.Collect();
                        GC.WaitForPendingFinalizers();
                        GC.Collect();
                        GC.WaitForPendingFinalizers();
                    }
                    catch { }
                    finally
                    {
                        Environment.Exit(0);
                    }
                });
            }
            catch { }
            finally
            {
                try
                {
                    webView.Dispose();
                }
                catch { }

                try
                {
                    threadRunner.Dispose();
                }
                catch { }
            }


Please advise what we need to fix.

Thanks!
eo_support
Posted: Monday, March 1, 2021 11:30:32 AM
Rank: Administration
Groups: Administration

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

You should not try to dispose the WebView or the ThreadRunner inside the ThreadRunner.Send method. You only need to destroy the WebView afterwards. So the code should be something like this:

Code: C#
var threadRunner = new ThreadRunner();
var webView = threadRunner.CreateWebView();

threadRunner.Send(() =>
{
    ....do something with the WebView
});

webView.Destroy();
threadRunner.Stop();


Please let us know if this works for you.

Thanks!
Omri Suissa
Posted: Monday, March 1, 2021 12:14:31 PM
Rank: Member
Groups: Member

Joined: 2/18/2020
Posts: 24
The threadRunner.Send will block the thread?
eo_support
Posted: Monday, March 1, 2021 12:45:21 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,217
Yes. That's the whole purpose of Send. If you do not want it to block, you use Post.
Omri Suissa
Posted: Tuesday, March 2, 2021 11:44:38 AM
Rank: Member
Groups: Member

Joined: 2/18/2020
Posts: 24
Thank you, however, we still get the error.
The updated code:
Code: C#
var threadRunner = new ThreadRunner();
            var webView = threadRunner.CreateWebView();

            try
            {
                webView.CertificateError += (s, e) => { e.Continue(); };
                webView.Resize(1920, 1000);

                threadRunner.Send(() =>
                {
                    try
                    {

                        try
                        {
                            foreach (var item in myCookies.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries))
                            {
                                try
                                {
                                    var nameAndValue = item.Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries);
                                    if (nameAndValue.Length > 1)
                                    {
                                        webView.Engine.CookieManager.SetCookie(new EO_WebEngine.Cookie(nameAndValue[0], nameAndValue[1]));
                                    }
                                }
                                catch { }
                            }
                        }
                        catch { }
                        
                        var navigation = webView.LoadUrl(myUrl);
                        navigation.WaitOne(10 * 1000);

                        if (navigation.HttpStatusCode == 200)
                        {
                            Thread.Sleep(3000);

                            var html = webView.EvalScript("document.documentElement.outerHTML", false).ToString();

                            if (!string.IsNullOrWhiteSpace(html))
                            {
                                var url = webView.EvalScript("document.location.href", false).ToString();

                                if (!string.IsNullOrWhiteSpace(url))
                                {
                                    // some logic
                                }
                            }
                        }
                    }
                    catch { }
                });
            }
            catch { }
            finally
            {
                try
                {
                    webView.Destroy();
                    webView.Dispose();
                }
                catch { }

                try
                {
                    threadRunner.Stop();
                    threadRunner.Dispose();
                }
                catch { }
            }

            try
            {
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            catch { }
            finally
            {
                Environment.Exit(0);
            }
eo_support
Posted: Tuesday, March 2, 2021 3:38:30 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,217
We tested your code here (without the cookie part) on the latest version and it works fine. Which version of our DLLs are you using?
Omri Suissa
Posted: Wednesday, March 3, 2021 3:25:28 AM
Rank: Member
Groups: Member

Joined: 2/18/2020
Posts: 24
I don't think it is consistent.
It happens on the production server "from time to time".
I'm using v20.2.34.0.
eo_support
Posted: Wednesday, March 3, 2021 10:31:44 AM
Rank: Administration
Groups: Administration

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

I am not sure what else to tell you then. If you continue to have problem, you can try to isolate the problem into a test project and send the test to us. See here for more details:

https://www.essentialobjects.com/forum/test_project.aspx

Once we have the test project, we will investigate further.

Thanks
Omri Suissa
Posted: Sunday, March 7, 2021 2:31:31 AM
Rank: Member
Groups: Member

Joined: 2/18/2020
Posts: 24
Since it happens from time to time I don't have a way to reproduce it.
I thought that this is related to the fact that I'm using environment.exit
eo_support
Posted: Tuesday, March 9, 2021 2:33:35 PM
Rank: Administration
Groups: Administration

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

We will need a test project to investigate further. You can send the test project to us even if it only occurs from time to time. For example, if it happens once after you run the test project 10 times, then it will still be very useful because we can still use it to reproduce the problem.

Thanks


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.