Page 1 of 1

wxURL does not work in wxThread?

Posted: Thu Oct 14, 2004 11:08 am
by fov
Dear everybody,

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 :D. 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)

This is a known kinda thing...

Posted: Thu Oct 14, 2004 1:00 pm
by caseyodonnell
Try reading:
A thread on groups.google.com.
Another possibly useful google.groups.com thread.

That might offer some insight.

A little shameless self-promotion...wxCurl should work just fine how you've done it already. wxSocket based classes need the event loop. wxCurl does not. (There is a wxCurl thread in the components area if you have trouble...makes me wonder...have I checked in recently?)

CKO

Posted: Thu Oct 14, 2004 2:27 pm
by Ryan Wilcox
fov,

Speaking of wxCurl again, If the idea is to show progress, wxCurl has a wxCurlProgressEvent event that you use just like any wxWidgets event. (There's even documentation on it! yay!).

You could also use this event to show progress and take care of housekeeping things too (writing the downloaded file to a, umm, local file on the hard disk, or whatever).


(and, Casey, there's a components area? :shock: news to me :? maybe you do need to check in...)

Posted: Thu Oct 14, 2004 2:40 pm
by caseyodonnell
Hehehe...sorry...I'm being flakey this morning.

In the forum, components area...there is a wxCurl topic that I pay attention to.

I did check in updated VC6 project files this morning...updated to reflect changes in curl that were causing MSW users to get winsock2.h includes and freaky warnings/linker errors.
Ryan Wilcox wrote: (and, Casey, there's a components area? :shock: news to me :? maybe you do need to check in...)

Posted: Thu Oct 14, 2004 4:10 pm
by fov
Uum, well, I don't understand the internals of wx yet, but from what I read in your links, my problem might be related to the missing event loop in a thread? Then why does it work on Linux systems? I also tried wxWindows 2.5.3 (zip snapshot from download page) today and it also works fine on Win32.

I see that wxCurl may be more powerful, but actually I don't want to use another additional package at the moment. And I also don't really want to go singlethreaded again, because I now made several worker classes which are all multi threaded. It would be too much work to make them singlethread again.

Isn't there an easy way to get it to work with plain wxWindows 2.4.x? If it's a missing event loop, it should be easy to do some event calls in the thread, shouldn't it? Sorry, I don't know the internals, and so I don't really know how to continue now.

Posted: Thu Oct 14, 2004 8:11 pm
by SnakeChomp
fov wrote:I also tried [wxWidgets] 2.5.3 (zip snapshot from download page) today and it also works fine on Win32.
If 2.5.3 works on windows and in linux, the problem is solved, is it not? Why not just use 2.5.3?

Posted: Fri Oct 15, 2004 2:15 pm
by fov
because 2.5.3 is announced to be a development snapshot while 2.4.2 is said to be the stable release

Posted: Fri Oct 15, 2004 7:58 pm
by SnakeChomp
fov wrote:because 2.5.3 is announced to be a development snapshot while 2.4.2 is said to be the stable release
That doesn't make any difference. Just because its a "Development snapshot" does not mean that it will crash every 5 seconds. It just means that certain new functions and or new classes may be altered and or removed before the next "stable release." There must have been a bug preventing what you are doing from working in 2.4.2, and has since been fixed in development up to 2.5.3. They aren't going to fix it again and re-release 2.4.2. There is no real reason for you not to use 2.5.3, especially if it fixes a bug you are encountering.