Solved

Kill a thread after a time limit

Posted on 2001-07-14
4
286 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

867 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

12 Experts available now in Live!

Get 1:1 Help Now