Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Python, Lua and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Java, C, C++, Ruby, MySQL and Linux / Tomcat systems
Killing process generated by separate thread

Posted by Dimon (Dimon), 21 July 2005
While writing a shell for my console c++ program,  find a problem with killing process. Tcl script opens new thread which generates new process for reading data from my C program:

set listener [thread::create {    thread::wait
thread::send -async $listener [SendCmd]

SendCmd script is:
if [catch {open "|$directive |& cat"} pipe]  {
   ConsoleArea insert end $pipe\n
} else {
   fileevent $pipe readable WriteLog
   ConsoleArea insert end $directive\n
   ConsoleArea see end

directive - command line
cat - program for catching errors
ConsoleArea - Text widget for showing text information returned by C++ program

WriteLog is a script for logging and shutting down thread:
if [eof $pipe] {
   catch {close $pipe}
   if [info exists listener] {
       thread::release $listener
} else {
   gets $pipe line
   ConsoleArea insert end $line\n
   ConsoleArea see end

When I call process::release $listener later, I successfully kill the thread but program that was run in that thread still work.
According to TCL manual, in UNIX to killing a process  possible with kill command.
How can I do it in Windows 2000 ?

OS: Windows 2000 Pro SP4
Tcl version: 8.4.2 with thread support
Tk version 8.4.2 with thread support
Both compiled by my own.

Posted by admin (Graham Ellis), 27 July 2005
I'm not familiar with killing processes under Windows ... I specialise in the Open Source programming languages themselves, with some Unix and Linux thrown in.   I was hoping that someone else might chime in with and answer here but ... sorry ... I don't think any of the other regulars know either.

Anyway - this follow up post at least assures you that we've looked at the question and will also bump it to the top of the pile to highlight it again.

Posted by neo (The Architect), 28 July 2005
You would have to use some third party tool for this I think.
I googled for it and this looks like a possible tool for it

Posted by neo (The Architect), 28 July 2005
Ok I found it. You do not need to have a third party tool.
Windows XP has a command "tskill" for this. I think on some other versions of Windows its called "taskill".
Just check and let us know.

Posted by Dimon (Dimon), 9 August 2005
All hail, gentlemen!
Thank you ever so much for your attention for my question. I try to search any command like "taskill" in Windows 2000 Pro but don't found it yet. But I tried an alternative way to solve my problem and wrote small program witn C language that using TerminateProcess() WIN API function to kill a process. But I found a second problem with determine a process handle in TCL.  

if [catch {open "|$::testing::directive |& cat"} ::testing::pipe]  {
   ConsoleArea insert end $::testing::pipe\n
} else {
   set pids [pid $::testing::pipe]

The pid function returns me a process handle in uncustomary format for me: it seems like "123 4567".

How can I aright encode this format to accustomed hex view to send this handle correctly in TerminateProcess() WIN API function?

Posted by Dimon (Dimon), 6 October 2005
Well, I return to this task and solve the problem.

In this code snippet:
if [catch {open "|$directive |& cat"} ::testing::pipe]  {
   ConsoleArea insert end $::testing::pipe\n
} else {
   set pids [pid $::testing::pipe]

pids - the process Id seems like "123 4567".
So I take a series of experiments and looked accurately at the information about PIDs in Windows 2000 Task Manager. I have found that  1-st part of $pids is a true process identifier!
But I don't know about purpose of second part yet.

To kill a process spawned in separate thread I done the next.

I call thread::release function to release the thread.
After that I can to kill the process spawned in thread.

I call a small C++ program that kills a process and send as a parameter 1-st part of "pids"  variable's  value:

#include "stdafx.h"
#include <windows.h>
#include <stdlib.h>

int _tmain(int argc, _TCHAR* argv[])
 // argv[1] is a real PID
 UINT dwProcessId = atoi(argv[1]);
 HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, dwProcessId);
 if (hProcess == NULL) return FALSE;
 if (!TerminateProcess(hProcess, (DWORD)-1)) dwError = GetLastError();
 return dwError == ERROR_SUCCESS;

I think the next step would be a wrapping this C++ function as Tcl-extention.

That's all

This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2018: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01225 708225 • FAX: 01225 793803 • EMAIL: • WEB: • SKYPE: wellho