For Programmers: Free Programming Magazines  


Home > Archive > VC Language > November 2005 > Terminate a thread









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Terminate a thread
Si

2005-11-17, 7:05 pm

I have a worker thread that uses CHttpFile to do some client-server
communication. If a CInternetException is detected during that thread
running I want to stop the thread from running. How do I do this?

I also want to terminate the thread if the dialog (in which the thread runs)
is closed. How do I do this?


AliR

2005-11-17, 7:05 pm

Worker threads exit when the thread function exits.

So,

> I have a worker thread that uses CHttpFile to do some client-server
> communication. If a CInternetException is detected during that thread
> running I want to stop the thread from running. How do I do this?


Exit the thread function.

>
> I also want to terminate the thread if the dialog (in which the thread

runs)
> is closed. How do I do this?
>
>

Set a flag in the thread class, have the thread function check it every once
in a while, if flag is set, exit the thread function.


Si

2005-11-17, 7:05 pm

"AliR" <AliR@online.nospam> wrote in message
news:%k7ff.22358$dO2.17878@newssvr29.news.prodigy.net...
> Worker threads exit when the thread function exits.


So to exit the function do I just use return; ?

> So,
>
>
> Exit the thread function.


The CInternetException occurs in an object created in the thread, so how do
I get the thread function to exit from within an object within the thread?

> runs)
> Set a flag in the thread class, have the thread function check it every
> once
> in a while, if flag is set, exit the thread function.


Using a Timer?


RobKinney1

2005-11-17, 7:05 pm

I think you want to use return 0; if you terminate cleanly inside the worker
thread.

Rob

"Si" wrote:

> "AliR" <AliR@online.nospam> wrote in message
> news:%k7ff.22358$dO2.17878@newssvr29.news.prodigy.net...
>
> So to exit the function do I just use return; ?
>
>
> The CInternetException occurs in an object created in the thread, so how do
> I get the thread function to exit from within an object within the thread?
>
>
> Using a Timer?
>
>
>

Joseph M. Newcomer

2005-11-21, 9:58 pm

UINT CMyThread::RunThread(LPVOID p)
{
TRY
{
.... do work here, such as calling functions
}
CATCH(CInternetException, e)
{
...do stuff required to clean up the state
e->Delete();
}

return 0;
}

Note that instead of doing e->Delete() you can simply do throw; in inner contexts so you
can clean up other things, such as closing the connection, freeing storage, etc.
joe

On Thu, 17 Nov 2005 22:34:31 GMT, "Si" <si@abc.com> wrote:

>"AliR" <AliR@online.nospam> wrote in message
>news:%k7ff.22358$dO2.17878@newssvr29.news.prodigy.net...
>
>So to exit the function do I just use return; ?
>
>
>The CInternetException occurs in an object created in the thread, so how do
>I get the thread function to exit from within an object within the thread?
>
>
>Using a Timer?
>

Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com