| |

VerySource

 Forgot password?
 Register
Search
View: 1250|Reply: 10

Questions about ending threads

[Copy link]

1

Threads

6

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-1-22 09:20:01
| Show all posts |Read mode
Excuse me, I want to download a file. During the download process, I want to end the thread halfway and use TerminateThread (); but Downloadfile.close () has not run yet.
UINT Download (void * pArg)
{
        CFile DownloadFile;
DownloadFile.Open (strDirectory, CFile :: modeCreate | CFile :: modeWrite | CFile :: modeNoTruncate);

while (nCompletedSize <nFileSize)
{


nReceSize = HttpSocket.Receive (pData, 1024);
DownloadFile.Write (pData, nReceSize);
nCompletedSize + = nReceSize;
Ranch
}
DownloadFile.Close ();
}
Reply

Use magic Report

0

Threads

57

Posts

27.00

Credits

Newbie

Rank: 1

Credits
27.00

 China

Post time: 2020-2-1 08:45:02
| Show all posts
Add a flag condition in while, set the value to be exited in the main thread, and wait
Reply

Use magic Report

1

Threads

6

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-2-14 10:30:01
| Show all posts
Because it often runs to HttpSocket.Receive (pData, 1024); it will get stuck there, so I use forced exit, is there any good way?
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-2-14 20:15:01
| Show all posts
Because it often runs to HttpSocket.Receive (pData, 1024); it will get stuck there, so I use forced exit, is there any good way?

-------------------------------------------------- -----------
Stuck there because your revive is still waiting to be accepted, but the server does not have a send corresponding to it, so it has been "stuck" there, and your code for receiving files should be a bit problematic. This is not a problem that forced exit can solve.
Reply

Use magic Report

1

Threads

6

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-2-16 09:15:01
| Show all posts
No, receive does not get stuck under normal circumstances, I think it is when the receive card makes him quit at that time, the issue of receive is not discussed here
Reply

Use magic Report

1

Threads

6

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-2-16 09:30:01
| Show all posts
It is normal to receive here, and it will only get stuck in special times. Here, we will not discuss the receive problem, but just how to solve the problem that did not close Downloadfile.close ()
Reply

Use magic Report

0

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 Invalid IP Address

Post time: 2020-2-16 19:00:01
| Show all posts
For forced exit, you can use endThread. This function is also used in the thread. However, this method is not recommended.
Generally use TerminateThread ();
Here are two functions for your reference:
void MyThread :: WaitForThreadToTerminate (HANDLE hThread) // Wait for the thread to terminate
{
DWORD dwRet;
do
{
dwRet = :: MsgWaitForMultipleObjects (1,&hThread, FALSE,
INFINITE, QS_ALLINPUT);
if (dwRet! = WAIT_OBJECT_0)
{
PeekMessageLoop ();
}
} while ((dwRet! = WAIT_OBJECT_0)&&(dwRet! = WAIT_FAILED));
}

void MyThread :: PeekMessageLoop ()
{
MSG msg;
while (PeekMessage (&msg, NULL, NULL, NULL, PM_REMOVE))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
}




MyThread :: ~ MyThread ()
{

EndThread_Proc ();
if (bEndTDde)
{
WaitForThreadToTerminate (pMyThread-> m_hThread);
}

}

void MyThread :: EndThread_Proc ()
{
bEndTDde = True;
}

CWinThread * pMyThread;
BOOL bEndTDde;

I hope useful to you.
Reply

Use magic Report

1

Threads

6

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-2-16 21:30:01
| Show all posts
Thank you upstairs, but how do I solve the problem that did not turn off downloadfile.close ()
Reply

Use magic Report

1

Threads

6

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-6-19 18:45:01
| Show all posts
Sorry, the front is not Receive, it should be .recv(); I heard that it is caused by blocking mode, how to solve it?
Reply

Use magic Report

0

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-8-15 21:00:02
| Show all posts
The threads in my code wait until the resources are used up before ending.
At the end you modify the condition of while(nCompletedSize <nFileSize), the program should be able to execute to DownloadFile.Close();
But you may need to use try catch to handle the error.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list