suggestions for a wxDownloadThread
Vadim Zeitlin
vadim at wxwindows.org
Sun Sep 3 15:56:59 PDT 2006
On Mon, 04 Sep 2006 00:06:34 +0200 Francesco Montorsi <f18m_cpp217828 at yahoo.it> wrote:
FM> 3) I want to be able to eventually abort a download once it has started
FM>
FM> Can I use a detached thread and still be able to get feature #3?
Yes.
FM> AFAIU a detached thread should never be tested:
FM>
FM> myDetachedThread *p = new myDetachedThread;
FM> p->GetSomething(); // ERROR! the thread could not exist anymore!!!
FM>
FM> right?
Right, you need to test for the thread existing before using it. In the
simplest case, a global variable (suitably protected by a mutex/critical
section) could be used but, of course, using globals is not usually a good
idea.
FM> So, how can I communicate from the main thread to a detached thread ?
FM> Detached thread -> main thread communication can be done through events
FM> but I wonder how can I communicate from the main thread the "abort"
FM> command to the detached thread...
Again, the simplest thing is to set a flag somewhere which the thread
checks periodically and aborts if needed. Like this you don't even need to
worry about the above.
FM> BTW: I think that a wxDownloadThread with the 3 features above should
FM> really be part of wxWidgets as anyone using a GUI and in need to
FM> download a file needs to wrap wxURL::GetInputStream in a separate thread
FM> to avoid blocking the application. Maybe I could submit it as a patch to
FM> wx when it's done?
I think it would be useful to have a generic thread class which
a) can be interrupted easily and safely
b) sends an event on completing what it does
I.e. wxDownloadThread should be a simple subclass of this (let me
tentatively name it wxGUIWorkerThread) class where you'd just override a
single virtual DoWork() function to run the download in it (periodically
calling TestDestroy()).
Regards,
VZ
--
TT-Solutions: wxWidgets consultancy and technical support
http://www.tt-solutions.com/
More information about the wx-users
mailing list