Solved

Kill a thread after a time limit

Posted on 2001-07-14
4
285 Views
Last Modified: 2013-12-03
I have a program that starts a number of threads.

Each thread gets some data and does some manipulation to get a "result"

When each thread completes it sends a message to the window that started it, telling it's result.

Unfortunately there is code in the thread that very occassionally (like 1 in 10,000 times or less) can hang, and the result never comes back.  I am not in a position to fix this code.  

Killing the hung thread and trying the calculation again, is the best (only) way to solve this.

What I need is a SIMPLE way to kick off a thread, and if the thread hasn't completed in a set amount of time (say 30 seconds), kill it, and try again.

Suggestions?

No MFC.  C/C++ Win32 using Visual C++ v6. FWIW this stuff lives in a DLL
0
Comment
Question by:Answers2000
4 Comments
 
LVL 32

Expert Comment

by:jhance
ID: 6282275
Set a watchdog timer in your main process when you start the thread.  When the timer expires check the status of the thread.  If it's still running, kill it and do your thing again.

I do find it odd that a thread doing some sort of calculation would hang.  I think a better approach would be to fix the code that is hanging so that it doesn't hang.
0
 
LVL 5

Accepted Solution

by:
robpitt earned 100 total points
ID: 6282402
As Jhance says you need to write a simple thread management routine that runs periodically.

Calling TermianteThread() is bound to cause a resource leak. This might be tolerable in a 1 in 10000 scenario.

However if it happens a lot you would be wise to move over to having a seperate server process - this way your main application is isolated from the leaks in the server.

Rob
0
 
LVL 30

Expert Comment

by:Axter
ID: 6288031
The following functions has four different methods used to close a program.  The last two methods can be used to close a thread.

void CreateProcessMngr::CloseApp(void)
{
::PostMessage(m_hWnd, WM_SYSCOMMAND,(WPARAM)SC_CLOSE,0);
::CloseHandle(pi.hProcess);
::TerminateThread(pi.hThread,0);
::TerminateProcess(pi.hProcess,0);
}

You can find the complete CreateProcessMngr class in the following link:
http://www.axter.com/faq/topic.asp?TOPIC_ID=24&FORUM_ID=4&CAT_ID=9&Topic_Title=How+to+launch+a+program+from+another+program+in+VC&Forum_Title=C%2FC%2B%2B
0
 
LVL 8

Author Comment

by:Answers2000
ID: 6352353
jhance, actually it involves a network op.  And I'm not in a posiiton to fix it, even if I can duplicate it.  With such a rare occurence you gotta leave the PC running for days to get one

I actually found another way of approaching the problem which solves the issue in a totally different way.  If I had gone this route, then robpitt's TerminateThread would have been the key, so I'll give him the pts fo close the question
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now