Network functions within wxFrame constructor (solved)

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
ovh
Earned a small fee
Earned a small fee
Posts: 10
Joined: Tue Mar 22, 2005 3:43 pm

Network functions within wxFrame constructor (solved)

Post by ovh » Tue Mar 22, 2005 3:50 pm

Hi all :)

I'm writing a little network client app and I need to read some data accross the network while the app initializes. So I wrote the code in the main frame constructor but it doesn't work :( If I place the exact same code within the event function of a button, it works perfectly well when I click that button... :shock:

So is there some limitation to use network functions in GUI constructors and how can this be solved?

Thanks for any help :)
Last edited by ovh on Tue Apr 05, 2005 7:22 pm, edited 1 time in total.
I'm nothing to do with the hosting company www.ovh.com

koderpat
Experienced Solver
Experienced Solver
Posts: 62
Joined: Tue Apr 05, 2005 5:04 am
Contact:

Post by koderpat » Tue Apr 05, 2005 5:13 am

What about using wxThreads? or wxTimer?

upCASE
Site Admin
Site Admin
Posts: 3176
Joined: Mon Aug 30, 2004 6:55 am
Location: Germany, Cologne

Post by upCASE » Tue Apr 05, 2005 5:29 am

Hi!
So I wrote the code in the main frame constructor but it doesn't work
Could you please be a little bit more specific? What seems to be the exact problem? Does the app crash? In general it will be easier for all of us to comment if you post your error log and maybe some code.

Be sure to use wxSocketBase::Initialize() in your wxApp instance.
OS: OpenSuSE, Ubuntu, Win XP Pro
wx: svn
Compiler: gcc 4.5.1, VC 2008, eVC 4

"If it was hard to write it should be hard to read..." - the unknown coder
"Try not! Do. Or do not. There is no try." - Yoda

ovh
Earned a small fee
Earned a small fee
Posts: 10
Joined: Tue Mar 22, 2005 3:43 pm

Post by ovh » Tue Apr 05, 2005 1:04 pm

First of all thanks for your help guys :)

I don't need threads for this very simple app, timer yes I thought about it, I'll try if anything else fail. ;)

No the app doesn't crash at all but it doesn't receive any response from the server. I didn't know the wxSocketBase::Initialize() it was not written in the 2.4.x doc but I saw it in the 2.5.x lol :lol: I added this line but it doesn't solve the issue.

Now I explore the wx socket sample carefully to see if I do something wrong...
I'm nothing to do with the hosting company www.ovh.com

Cursor
Earned some good credits
Earned some good credits
Posts: 120
Joined: Sun Aug 29, 2004 3:09 pm
Location: Grenoble, France
Contact:

Post by Cursor » Tue Apr 05, 2005 2:00 pm

Hello.

The wxSocket* must be executed after all GUI creation at the begining of any application.
IMHO, it is because some wxSocket* internal initialization are done after GUI creation. So if you can, put your code into a function called from a menu (or a button). Or if you must have an automatic wxSocket start, you can POST an event in the constructor of the main frame witch will be processed after all init.
What is little and green, witch go up and down ??
Yoda playing with the force.

ovh
Earned a small fee
Earned a small fee
Posts: 10
Joined: Tue Mar 22, 2005 3:43 pm

Post by ovh » Tue Apr 05, 2005 2:45 pm

Cursor wrote:Or if you must have an automatic wxSocket start, you can POST an event in the constructor of the main frame witch will be processed after all init.
Mmmmm very interesting, you mean create my own custom event? I'll explore that way, thanks :)
http://www.wxwidgets.org/manuals/2.5.5/ ... stomevents
I'm nothing to do with the hosting company www.ovh.com

Cursor
Earned some good credits
Earned some good credits
Posts: 120
Joined: Sun Aug 29, 2004 3:09 pm
Location: Grenoble, France
Contact:

Post by Cursor » Tue Apr 05, 2005 3:07 pm

You must not create a new type of event. You can use an existing event (a wxCommandEvent used with a wxEVT_COMMAND_MENU_SELECTED event type is ok. But you must post it and not process it immediatly.

http://www.wxwidgets.org/manuals/2.5.4/ ... ndingevent
http://www.wxwidgets.org/manuals/2.5.4/ ... xpostevent
What is little and green, witch go up and down ??
Yoda playing with the force.

ovh
Earned a small fee
Earned a small fee
Posts: 10
Joined: Tue Mar 22, 2005 3:43 pm

Post by ovh » Tue Apr 05, 2005 3:21 pm

Waw ok! Thank you very much for your detailed help :D =D>
I'm nothing to do with the hosting company www.ovh.com

ovh
Earned a small fee
Earned a small fee
Posts: 10
Joined: Tue Mar 22, 2005 3:43 pm

Post by ovh » Tue Apr 05, 2005 7:21 pm

Many thanks Cursor, your tip with wxPostEvent works perfectly! :D

Extract of my code:

Code: Select all

BEGIN_EVENT_TABLE(MainWin, wxFrame)
	// (...)
	// customized event, posted after initialization
	EVT_MENU(POSTEVENT, MainWin::OnPostinit)
END_EVENT_TABLE()

// in the main frame constructor:
wxCommandEvent myevent(wxEVT_COMMAND_MENU_SELECTED, POSTEVENT);
wxPostEvent(this, myevent);

void MainWin::OnPostinit(wxCommandEvent &e)
{
    // ... do my network stuff here
}
And now my network routine is executed right after the GUI has loaded, as I wish! Thank you again you rocks 8)
I'm nothing to do with the hosting company www.ovh.com

Post Reply