I'm new to wxWidgets. I'm trying things for 4 weeks now and I like it more and more every day. Especially the control sizers are incredible easy and powerful to use . However I encountered a problem that I wasn't able to solve yet. I'm trying to use wxURL to retrieve a webpage. It works fine as long as I use it in the main thread. If I try to use it in another thread, GetInputStream never returns (and no outgoing connection is made).
I'm using wxWidgets 2.4.2 stable release and encounter the problems on Win32 systems where I use the latest release MinGW compiler. I already tried to upgrade to g++ 3.4.2 and the newer w32headers (latest release candidates), but that doesn't make a difference. Though I haven't tried using VC yet (which isn't an option for my project anyway, because VC is not free). Strange thing is, that the same program runs fine on Linux using wxGTK 2.4.2.
Here's a little test program I wrote to demonstrate the problem. Setting USE_THREAD to 0 works fine, using 1 or 2 does never print "Got stream pointer" on Win32.
Does anybody know what I did wrong?
Code: Select all
#include <wx/wx.h>
#include <wx/url.h>
// 0 = don't use thread
// 1 = use detached thread
// 2 = use joinable thread
#define USE_THREAD 1
class MyThread :public wxThread
{
public:
MyThread () :wxThread((USE_THREAD == 2) ? wxTHREAD_JOINABLE : wxTHREAD_DETACHED)
{
}
ExitCode Entry ()
{
wxURL url("http://www.google.com/");
wxMessageBox("URL object created");
wxInputStream *stream = url.GetInputStream();
wxMessageBox("Got stream pointer");
char buf[1024];
stream->Read(buf, 1023);
buf[1023] = '\0';
wxMessageBox(buf);
return 0;
}
};
class MyApp :public wxApp
{
public:
bool OnInit ()
{
wxMessageBox("Starting");
MyThread thread;
#if USE_THREAD == 2
thread.Create();
thread.Run();
thread.Wait();
#elif USE_THREAD == 1
thread.Create();
thread.Run();
wxSleep(5);
#else
thread.Entry();
#endif
wxMessageBox("Finished");
return false;
}
};
IMPLEMENT_APP(MyApp)