Solved

Kill a thread after a time limit

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Table Size and Indexes in SQL Server 6 24
Clean Uninstall of VS Professional 2015 5 52
Need to create an object factory 2 46
WPF issue with Trigger 2 106
This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
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 Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
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…

777 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