Search through list of tasks in task manager

M

Marcus

Hi,

I am a newbie at C#. Here is my current problem:

I want my application to iterate through the list of tasks presented in
task manager. I want it to look at the task names and if a certain task
exist in the task list I want to kill it (as nice as possible).

I started the task manager with the following row:
System.Diagnostics.Process.Start("c:\\windows\\system32\\taskmgr.exe");

I then get the handle to the task manager by doing (Win32 is a
DllImport of user32.dll):
int iHandle = Win32.FindWindow(null, "Windows Task Manager");



Can I use the EnumChildWindows, GetWindowText, GetClassName (all of
them from the windows 32 API) for getting the handle to the
Applications tab (and thereby get to the list of tasks)? or how do I go
about?
 
G

Guest

It sounds to me you are going the long way to get what you want. Take a look
at the Process class in the System.Diagnostics namespace.

The class has a static method called GetProcessesByName, if it appears in
the array it returns then voila it is a running process. You can then call
the Kill method on it.
 
M

Marcus

Thanks a lot for answering, I really appriciate it.

I am probably going the long way (which I dont really want), so thanks
again for your help.

I wonder if I maybe used the wrong word when I said I wanted to kill a
task. What I really want is to kill an application (or is it the same
thing?)
As far as I know an application can consist of several processes.

What I want to do is to first retrieve all the application names and if
I find for instance the substring "Microsoft Word" as part of an
application name, I want to kill that application as gentle as
possible.

So I guess the first thing I need to do is get the running application
names (how can I do that), and then if I find an application that I
want dead, I need to find out which processes are related to that
application name and kill those (or is there methods of doing this at
application level instead of at process level)

Is there a good solution to this one?
 
W

Willy Denoyette [MVP]

| Thanks a lot for answering, I really appriciate it.
|
| I am probably going the long way (which I dont really want), so thanks
| again for your help.
|
| I wonder if I maybe used the wrong word when I said I wanted to kill a
| task. What I really want is to kill an application (or is it the same
| thing?)
| As far as I know an application can consist of several processes.
|
| What I want to do is to first retrieve all the application names and if
| I find for instance the substring "Microsoft Word" as part of an
| application name, I want to kill that application as gentle as
| possible.
|
| So I guess the first thing I need to do is get the running application
| names (how can I do that), and then if I find an application that I
| want dead, I need to find out which processes are related to that
| application name and kill those (or is there methods of doing this at
| application level instead of at process level)
|
| Is there a good solution to this one?
|

The first question that comes to mind is why do you want to kill
applications?
There is simply no way to .... kill as gentle as ..., killing a process is a
rude operation, you always throw away all work done so far, is this really
what you are looking for?

Willy.
 
M

Marcus

Yes it is. I am used to the Unix world and in there you have got 2
different ways of terminating a process. Either you can make a kill or
you can make a kill -9. If you leave the -9 out of it, then you let the
OS handle the kill so that the killed process exits gracefully (files
held be the process closed in god manner), the process telling its
parent its exit status etc. If you do a kill -9 then you just brutally
kills it.
Kind of the same as when you close down Microsoft Windows (the OS
itself), you are sometimes asked to wait for some process to end
(kill), but you have the choice of terminating it right away (kill -9).
So to your question: I want to send the same message to the application
that would be sent if I did it the file->exit way. This be WM_CLOSE or
whatever is used, I dont know. I just want it automated from inside
another application's code (selection block or something alike).
 
G

Guest

I'm telling ya, everything you need is in the System.Diagnostics namespace in
the Process class.

If you want a process to exit "gracefully" then call the Close and/or
CloseMainWindow methods.

If you want to be rude about it, just call the Kill method. Here is a link,
watch for word wrap:
http://msdn.microsoft.com/library/d...l/frlrfSystemDiagnosticsProcessClassTopic.asp

If you want to execute the File -> Exit in a running windows application
then take a peek at the SendKeys class in the System.Windows.Forms namespace.
Here is a link, watch out for word wrap:
http://msdn.microsoft.com/library/d...frlrfsystemwindowsformssendkeysclasstopic.asp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top