NewTab from event thread

Having problems with building or using the CefGlue .NET/Mono binding? Ask your questions here.

Moderator: fddima

NewTab from event thread

Postby pssgcsim » Mon Jun 10, 2013 10:38 am

Hi I have problem with adding TabPage to TabControl in Windows Forms from event thread. In case like this one:

Calling event
Code: Select all
            browser.BeforePopup += (s, e) =>
            {
                NewTab(e.url);
            };

NewTab modifiers
Code: Select all
public void NewTab(string url)

Error showed by MS Visual Studio on line
Code: Select all
            _tabs.TabPages.Add(tabPage);
User avatar
pssgcsim
Techie
 
Posts: 13
Joined: Sat May 25, 2013 10:56 am

Re: NewTab from event thread

Postby fddima » Tue Jun 11, 2013 4:55 am

Run NewTab in correct thread (winforms UI thread).
fddima
Master
 
Posts: 788
Joined: Tue Dec 07, 2010 6:10 am

Re: NewTab from event thread

Postby pssgcsim » Tue Jun 11, 2013 6:36 am

And how can I do it from event please ?
User avatar
pssgcsim
Techie
 
Posts: 13
Joined: Sat May 25, 2013 10:56 am

Re: NewTab from event thread

Postby pssgcsim » Tue Jun 11, 2013 7:10 am

Thx I got it :D
Code: Select all
_pUIThread.Post((_state) => { NewTab(url); }, null);
User avatar
pssgcsim
Techie
 
Posts: 13
Joined: Sat May 25, 2013 10:56 am

Re: NewTab from event thread

Postby fddima » Tue Jun 11, 2013 7:41 am

pssgcsim wrote:Thx I got it :D
Code: Select all
_pUIThread.Post((_state) => { NewTab(url); }, null);

Yeah, correctly.
fddima
Master
 
Posts: 788
Joined: Tue Dec 07, 2010 6:10 am

Re: NewTab from event thread

Postby mistymorning » Tue May 06, 2014 1:59 pm

The way that I do this is to add a static referance to the control from Form_Load

WebBrowser.TabControl = this.TabPages1;

then from the code that needs to add a new tab page i switch to the UI thread by using

public static void NewTab(string url)
{//Called on the browser thread
WebBrowser.Url = url;
WebBrowser.TabControl .BeginInvoke(new Action(NewTab));
}

public static void NewTab()
{//Called on the UI thread
AddNewTab(WebBrowser.Url,true);
}


In addNewTab I create a new tabpage and then a browser control and add the borwser to the .tag of the new tabpage and then add the tabpage to the collection of pages in the tabcontrol thats on the form.

later I use a form timer to get the tabcontrol.selectedtab and cast the tag back to a browser control and then I can set other buttons on the main form using
WebBrowser B=(WebBrowser)TabPages1.selected.tag
CmdForward.Enabled=B.CanGoForwards;

You don't need to worry about cross thread errors on int or bool values but you might need locking on strings that are set from browser threads !!!
Building a web-browser that does not call home
mistymorning
Techie
 
Posts: 15
Joined: Sun May 04, 2014 8:08 am


Return to CefGlue Forum

Who is online

Users browsing this forum: No registered users and 15 guests