[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Kill a thread after a time limit

Posted on 2001-07-14
4
Medium Priority
?
294 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 300 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

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

649 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