cTrader Application crash calling WebBrowser control
cTrader Application crash calling WebBrowser control
23 Jul 2019, 21:32
I need to retrieve various data from servers and web pages in my robot. Some I can get using HtmlAgilityPack which works like a charm. Some pages provide data through scripts and it necessary to call them in the WebBrowser control. I have tested it in a separate application and I can get all data.
Now I have migrated this code into a robot. It compiles without errors and runs the first time. But as soon as I recompile it and call robot again, cTrader crashes completely. After restart this message is shown:
System.Runtime.InteropServices.COMException (0x80004005): Error HRESULT E_FAIL has been returned from a call to a COM component. at System.Windows.Forms.UnsafeNativeMethods.IWebBrowser2.Navigate2(Object& URL, Object& flags, Object& targetFrameName, Object& postData, Object& headers) at System.Windows.Forms.WebBrowser.PerformNavigate2(Object& URL, Object& flags, Object& targetFrameName, Object& postData, Object& headers) at System.Windows.Forms.WebBrowser.Navigate(String urlString) at cAlgo.Robots.MyRobotName.<RetrieveSignalData>b__6() at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart()
So, here is the stripped version of my code:
using System.Windows.Forms;
var th = new Thread(() => { var br = new WebBrowser(); br.ScriptErrorsSuppressed = true; br.Visible = false; br.DocumentCompleted += (s, e) => { fd = br.DocumentText; br.Stop(); }; br.ScriptErrorsSuppressed = true; br.Navigate("https://bla.com"); Application.Run(); }); th.SetApartmentState(ApartmentState.STA); th.Start();
I have found https://www.codeproject.com/questions/311319/a-problem-using-system-windows-forms-webbrowser-ob and https://social.msdn.microsoft.com/Forums/vstudio/en-US/0b89af2f-2884-461a-9d4f-6b1b0066a223/problem-with-my-webbrowser-hresult-efail-solved but no working solution.