Solved

Kill a thread after a time limit

Posted on 2001-07-14
4
291 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

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…
This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

717 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