Home » U++ Library support » U++ MT-multithreading and servers » How to close a Thread?
How to close a Thread? [message #38022] |
Wed, 28 November 2012 16:10 |
NilaT
Messages: 70 Registered: November 2011 Location: Austria
|
Member |
|
|
Hello,
I currently have a huge problem, hope you can help me.
I have a (Single<>)class which includes a Thread.
I need to use the thread from different positions of my main program.
The problem is, that I need to stop the thread before closing a dialog, but how can I do that? I searched on the site, in the forum, on google... But I found nothing usefull.
All I found was "ShutdownThreads", but this only sets a flag and does not stop the thread itself...
I also tried to Close the Handle after GetHandle(), with no success.
If you need some code, look here:
void TCPConnection::startChecking()
{
// Thread starten
if(s.IsOpen()) // s is a Socket Connection
{
m_terminateThread = false;
t.Run(THISBACK(cb));
}
}
void TCPConnection::stopChecking()
{
// thread beenden
if(t.IsOpen())
m_terminateThread = true;
}
void TCPConnection::cb()
{
while(!m_terminateThread)
{
tempBuffer = s.Read(1000);
if(!IsNull(tempBuffer))
{
buffer += tempBuffer;
tempBuffer.Clear();
WhenDataReceived();
}
}
// CloseHandle(t.GetHandle()); // wont work
bool TCPConnection::hasThreadStopped()
{
return !t.IsOpen();
}
And on the main program I do the following:
void xxxDlg::onCancel()
{
if(TcpConnection().s.IsOpen()) // TcpConnection() returns ref on Single<TCPConnection>
{
TcpConnection().stopChecking();
while(!TcpConnection().hasThreadStopped())
{
Sleep(50); // this is where I stuck... The thread is not closed!!!!!
}
TcpConnection().s.Close();
}
Break(IDCANCEL);
}
Thanks in advance
|
|
|
Goto Forum:
Current Time: Thu May 23 04:51:29 CEST 2024
Total time taken to generate the page: 0.01399 seconds
|