wxURL does not work in wxThread?

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
fov
In need of some credit
In need of some credit
Posts: 3
Joined: Thu Oct 14, 2004 10:53 am

wxURL does not work in wxThread?

Post 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)
caseyodonnell
Knows some wx things
Knows some wx things
Posts: 31
Joined: Fri Sep 10, 2004 1:03 pm
Location: Troy, NY
Contact:

This is a known kinda thing...

Post 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
Ryan Wilcox
I live to help wx-kind
I live to help wx-kind
Posts: 194
Joined: Mon Aug 30, 2004 1:26 pm
Location: PA, USA
Contact:

Post 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...)
Ryan Wilcox
Wilcox Development Solutions
http://www.wilcoxd.com
caseyodonnell
Knows some wx things
Knows some wx things
Posts: 31
Joined: Fri Sep 10, 2004 1:03 pm
Location: Troy, NY
Contact:

Post 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...)
fov
In need of some credit
In need of some credit
Posts: 3
Joined: Thu Oct 14, 2004 10:53 am

Post 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.
SnakeChomp
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 235
Joined: Sun Oct 10, 2004 2:53 am

Post 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?
fov
In need of some credit
In need of some credit
Posts: 3
Joined: Thu Oct 14, 2004 10:53 am

Post by fov »

because 2.5.3 is announced to be a development snapshot while 2.4.2 is said to be the stable release
SnakeChomp
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 235
Joined: Sun Oct 10, 2004 2:53 am

Post 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.
Post Reply