By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,652 Members | 1,128 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,652 IT Pros & Developers. It's quick & easy.

Search through list of tasks in task manager

P: n/a
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\\sys tem32\\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?

Feb 15 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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.

--
-Demetri
"Marcus" wrote:
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\\sys tem32\\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?

Feb 15 '06 #2

P: n/a
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?

Feb 16 '06 #3

P: n/a

"Marcus" <ma************@koping.net> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
| 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.
Feb 16 '06 #4

P: n/a
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).

Feb 16 '06 #5

P: n/a
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/de...ClassTopic.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/de...classtopic.asp

--
-Demetri
"Marcus" wrote:
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).

Feb 16 '06 #6

P: n/a
Thank you very much Demetri. I will look into this tonight!
Demetri skrev:
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/de...ClassTopic.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/de...classtopic.asp

--
-Demetri


Feb 16 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.