Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wxHTTP & Threads

I have some problems with using wxHTTP inside a Thread. I have created below class which derive from wxThread to use wxHTTP.

class Thread : public wxThread {
private:
wxHTTP get;

public:
Thread()
{
}
~Thread()
{
}

virtual ExitCode Entry()
{

    get.SetHeader(wxT("Content-Type"), wxT("text/html; charset=utf-8"));
    get.Connect(wxT("www.mysite.com"));

    get.SetTimeout(1);

    wxInputStream *httpStream = get.GetInputStream(wxT("/script.php?name=aaa&text=blabla"));
    wxDELETE(httpStream);
    get.Close();


    return 0;
}
};

I create this thread and run it (threads are created, ran and everything is fine with them). Unfortunately wxHTTP seems to doesn't work properly with threads (even my firewall doesn't ask me about connection). Is there any way to create wxHTTP connection inside a thread?

like image 654
fex Avatar asked Oct 09 '22 04:10

fex


2 Answers

Here is the answer (as requested by @bluefeet) wxHTTP inherits from wxSocketBase and in wxSocketBase we have this quote

When using wxSocket from multiple threads, even implicitly (e.g. by using wxFTP or wxHTTP in another thread) you must initialize the sockets from the main thread by calling Initialize() before creating the other ones.

See here for more explanation

like image 200
Stefano Mtangoo Avatar answered Oct 13 '22 11:10

Stefano Mtangoo


Call

wxSocketBase::Initialize();

in your apps OnInit function and wxurl/wxhttp functions should work from threads.

like image 23
Silver Moon Avatar answered Oct 13 '22 10:10

Silver Moon