From: "Gian Lorenzetto" on
Hi all,



I am using a wxThread derived class (created as JOINABLE) for background
processing, which the user can interrupt and I have a couple of questions.



I am waiting for the thread to finish by periodically calling IsAlive() from
a timer and then calling Wait() if IsAlive() returns false. From the
description in the wxWidgets manual I believe this is necessary to ensure
the thread releases all resources.



Now, if the user interrupts the process I call Delete() on the thread, which
stops the thread, but then calling Wait() returns an error and I receive the
system message:



"Couldn't terminate thread (error 6: the handle is invalid.)"



If I don't call Wait() after calling Delete(), everything works fine, but if
the manual is correct I am losing system resources by not calling Wait()?
So, I guess my question is, do I need to call Wait() after calling Delete()?
Or is there another way of stopping the thread?



Thanks,

Gian

From: Ryan Norton on
As I'm looking at the source I think that you are supposed to call
Wait() _instead_ of Delete() in the case of joinable threads

(Also, rather than what you are doing I recommend just sending a custom
event from the thread or some other method instead of checking it in a
timer)

[note to devs - the return values for Delete() and maybe other methods
are wrong in the docs as in actuality they return a wxThreadError in CVS
HEAD and not void]

Ryan


---------------------------------------------------------------------
To unsubscribe, e-mail: wx-users-unsubscribe(a)lists.wxwidgets.org
For additional commands, e-mail: wx-users-help(a)lists.wxwidgets.org

From: "Gian Lorenzetto" on

> As I'm looking at the source I think that you are supposed to call
> Wait() _instead_ of Delete() in the case of joinable threads

According to the doc (2.6.2), the two functions do very different things:

Delete():
"Calling Delete is a graceful way to terminate the thread. It asks the
thread to terminate and, if the thread code is well written, the thread will
terminate after the next call to TestDestroy which should happen quite
soon."

Wait():
"Waits until the thread terminates and returns its exit code or (ExitCode)-1
on error.
You can only Wait() for joinable (not detached) threads."

But I'm getting very different results. If I call Wait() on a running
(joinable) thread I immediately get a system error ("Couldn't terminate
thread (error 6: the handle is invalid)"). Calling Delete() does what I
expect - the thread terminates gracefully and dies.

Oddly, If the thread is dead (IsAlive() returns false) I can call Wait()
without a problem.

> (Also, rather than what you are doing I recommend just sending a custom
> event from the thread or some other method instead of checking it in a
> timer)

I'm using the timer to update the UI based on the progress of the thread
processing. I guess I could just fire an event from the thread after each
unit of work.

Gian



---------------------------------------------------------------------
To unsubscribe, e-mail: wx-users-unsubscribe(a)lists.wxwidgets.org
For additional commands, e-mail: wx-users-help(a)lists.wxwidgets.org

From: "Gian Lorenzetto" on
> > As I'm looking at the source I think that you are supposed to call
> > Wait() _instead_ of Delete() in the case of joinable threads

Looking through the source code, Wait() and Destroy() do appear to do the
same thing, except that detachable threads are deleted (the last parameter
to wxInternalThread::WaitForTerminate() is NULL for joinable threads).

After checking through my code again I was calling Delete() when the user
requests a stop to processing before the thread is finished and then calling
Wait(), generating the error.

I think the documentation for wxThread needs to be somewhat clearer as to
the uses of Delete() and Wait(). Currently the description of wxThread
includes:

"You shouldn't hurry to create all the threads joinable, however, because
this has a disadvantage as well: you *must* Wait() for a joinable thread or
the system resources used by it will never be freed, and you also must
delete the corresponding wxThread object yourself."

If, like myself you call Delete() to gracefully terminate the thread
*before* it is done and then think you must call Wait() to release the
thread resources, WaitForTerminate() is called twice. For me at least, this
generates an error.

Gian

>
> According to the doc (2.6.2), the two functions do very different things:
>
> Delete():
> "Calling Delete is a graceful way to terminate the thread. It asks the
> thread to terminate and, if the thread code is well written, the thread
will
> terminate after the next call to TestDestroy which should happen quite
> soon."
>
> Wait():
> "Waits until the thread terminates and returns its exit code or
(ExitCode)-1
> on error.
> You can only Wait() for joinable (not detached) threads."
>
> But I'm getting very different results. If I call Wait() on a running
> (joinable) thread I immediately get a system error ("Couldn't terminate
> thread (error 6: the handle is invalid)"). Calling Delete() does what I
> expect - the thread terminates gracefully and dies.
>
> Oddly, If the thread is dead (IsAlive() returns false) I can call Wait()
> without a problem.
>
> > (Also, rather than what you are doing I recommend just sending a custom
> > event from the thread or some other method instead of checking it in a
> > timer)
>
> I'm using the timer to update the UI based on the progress of the thread
> processing. I guess I could just fire an event from the thread after each
> unit of work.
>
> Gian
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: wx-users-unsubscribe(a)lists.wxwidgets.org
> For additional commands, e-mail: wx-users-help(a)lists.wxwidgets.org
>



---------------------------------------------------------------------
To unsubscribe, e-mail: wx-users-unsubscribe(a)lists.wxwidgets.org
For additional commands, e-mail: wx-users-help(a)lists.wxwidgets.org

From: "Gian Lorenzetto" on
Ryan,

Better, but I would still like to see mention of the fact that Wait()
requests that the thread end ASAP, and that there is no need to call
Delete() on a JOINABLE thread. This would fit nicely in the "wxThread
Deletion" section.

Actually, why does Delete() exist if Wait() does the same thing? (Or vice
versa?) Or is one only for detached threads?

Clearly I'm still confused :)

Gian

> -----Original Message-----
> From: Ryan Norton [mailto:wxprojects(a)comcast.net]
> Sent: Saturday, 18 February 2006 10:13 PM
> To: wx-users(a)lists.wxwidgets.org
> Subject: RE: wxThread
>
> >I think the documentation for wxThread needs to be somewhat clearer as
> > to
> > the uses of Delete() and Wait().
>
> Oh, I do too - I've attached my rewritten version (in HTML format)
> - let me know what you think (I've submitted it as a patch as well
> as I can always improve it later [if that is possible, I almost think
> I went a little overboard with the info already...]).
>
> RN


---------------------------------------------------------------------
To unsubscribe, e-mail: wx-users-unsubscribe(a)lists.wxwidgets.org
For additional commands, e-mail: wx-users-help(a)lists.wxwidgets.org