wxWebView can't use browser after wxSleep

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
userwebview
In need of some credit
In need of some credit
Posts: 6
Joined: Sun Nov 19, 2017 5:30 pm

wxWebView can't use browser after wxSleep

Post by userwebview »

I want to load some pages with wxwebview using ie engine (I set it to ie 11 ) and execute some javascripts on them but after some time since they loaded
so I created a thread to navigate to these pages (as I can't use wxSleep() in the main function ) and used wxSleep() after loading every page to execute some codes on it
the problem is that after using wxSleep() the browser becomes unusable and if I used it the program crashes and I get (from visual studio debugger) this error :

Exception thrown at 0x57D2EB9A (mshtml.dll) ....
and the debugger goes to line 914 in webview_ie.cpp and says this is the next line to execute when this thread return
this is my code :
1 - main.h

Code: Select all

#include <wx\wx.h>
#include <wx\webview.h>
#include <wx\thread.h>
#include <vector>

#define ID_WEB 598


class Navigator : public wxThread
{
public :
	Navigator(wxWebView *browse , wxString rurl , wxString rjs) : wxThread(wxTHREAD_DETACHED) {
		browser = browse;
		url = rurl;
		js = rjs;
	}
protected:
	wxWebView *browser;
	wxString url;
	wxString js;
	virtual ExitCode Entry();
};

class myFrame : public wxFrame
{
public:
	myFrame(wxString title);
protected:
	wxPanel *panel;
	wxBoxSizer *box;
	wxWebView *browser;
	Navigator *nav;
};
myFrame::myFrame(wxString title) : wxFrame(nullptr,wxID_ANY,title,wxDefaultPosition,wxSize(600,600)) {
	panel = new wxPanel(this, wxID_ANY, wxPoint(-1, -1));
	box = new wxBoxSizer(wxVERTICAL);
	browser = wxWebView::New(panel, ID_WEB);
	box->Add(browser, 1, wxEXPAND | wxALL,0);
	panel->SetSizer(box);
	wxString url = "https://stackoverflow.com";
	wxString js = "function aler(title) {alert(title);} aler(Alert);";
	nav = new Navigator(browser, url, js);
	nav->Create();
	nav->Run();
}
wxThread::ExitCode Navigator::Entry() {
	browser->LoadURL(url); // this works fine
	wxSleep(10);
	browser->RunScript(js); // the program crashes here with the error as I used wxSleep()
	return(wxThread::ExitCode*)0;
}
2- main.cpp

Code: Select all

#include "main.h"
using namespace std;
class App : public wxApp
{
public:
	bool OnInit();
	myFrame *frame;
};
bool App::OnInit() {
	frame = new myFrame("browser");
	frame->Show();
	return true;
}
wxIMPLEMENT_APP(App);
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4193
Joined: Sun Jan 03, 2010 5:45 pm

Re: wxWebView can't use browser after wxSleep

Post by PB »

Rule of a thumb: GUI classes are to be accessed only from the GUI (= primary) thread.

Can't you use wxTimer or the page loaded event (or when it comes to worst, CallAfter() assuming you are not interested in the RunScript's return value) instead?
userwebview
In need of some credit
In need of some credit
Posts: 6
Joined: Sun Nov 19, 2017 5:30 pm

Re: wxWebView can't use browser after wxSleep

Post by userwebview »

I passed this and used wxTimer which worked well but I got into another problem
I get sometimes an alert or confirm or prompt box and can't proceed without dealing with it but I can't do this in javascript
isn't there any method to pass these dialog boxes
and I heard that you are developing some classes to provide better javascript to wxWebView so will it be provided soon ?
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxWebView can't use browser after wxSleep

Post by ONEEYEMAN »

Hi,
I believe the JavaScript integration GSoC project is already in Git HEAD.
Try it.

If it fails - grab the appropriate GSoC branch and recompile everything.

Thank you.
userwebview
In need of some credit
In need of some credit
Posts: 6
Joined: Sun Nov 19, 2017 5:30 pm

Re: wxWebView can't use browser after wxSleep

Post by userwebview »

where is the link of the project of javascript
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxWebView can't use browser after wxSleep

Post by ONEEYEMAN »

Hi,
It is on the branch.
You need to clone the git tree of wxWidgets and then try with the Git HEAD and if it fails checkout the GSoC branch.

Thank you.
Post Reply