Solved

C# to End WINWORD.EXE task in task manager

Posted on 2007-04-10
8
5,516 Views
Last Modified: 2009-07-27
I want to write a C# vb.exe that that would kill couple of processes in my taskmanager under the Processes tab if they are running.
So I want to have the program schaduled to run every 30 minutes and it will look to see if OUTLOOK.EXE or WINWORD.EXE is running, if so run a END Task on them.

Thanks
0
Comment
Question by:AutomaticSlim
  • 4
  • 3
8 Comments
 
LVL 15

Accepted Solution

by:
igor_alpha earned 250 total points
ID: 18886503
Hi AutomaticSlim,

To kill process you can use array of Process objects:

Process[] processes = Process.GetProcessesByName("winword");
            foreach (Process process in processes)
            {
                process.Kill();
            }

Same thing with outlook process. Best way to shedule such program is using of standard windows built-in sheduler. Just set it to run every 30 minutes. Another way is creating your application as windows service but this way is more complicated,
0
 
LVL 2

Expert Comment

by:ironwill96
ID: 18886506
You are using C# or VB?

For VB would be like this:

Public Sub EnumerateProcesses()

Dim WindowsProcess As Process
Dim ProcessToKill As String = "AcroRd32"

For Each WindowsProcess In Process.GetProcesses
Debug.WriteLine(WindowsProcess.ProcessName)
If WindowsProcess.ProcessName = ProcessToKill Then
WindowsProcess.Kill()
End If
Next

End Sub

Then just call that from a Timer set to run every 30 * 60 * 1000 (if timer is in milliseconds).

For C# is very similar just change the syntax of the above code and it should work fine.  Also change the string to be your process name such as "winword".

This is found from this discussion thread: http://www.dotnet247.com/247reference/msgs/38/192027.aspx
0
 

Author Comment

by:AutomaticSlim
ID: 18890998
I use C#, I'll try the code today.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:AutomaticSlim
ID: 18891928
So added a little more to the code and it works. By any chance do guys know how I can  Restart The Server from the code?

 string[] strArray = new string[2] { "winword", "acrobat" };

            foreach (string sProcessName in strArray)
            {

                Process[] processes = Process.GetProcessesByName(sProcessName);
                foreach (Process process in processes)
                {
                    process.Kill();
                }

            }
0
 
LVL 15

Expert Comment

by:igor_alpha
ID: 18893933
It's quite another question....


An easy way is just to call the Shutdown.exe command with the Process.Start
method, passing the desired switches:

Shutdown the computer

Syntax

SHUTDOWN [logoff_option] [/m \\Computer] [options]

logoff_option:
/i Display the GUI (must be the first option) /l Log
off. This cannot be used with /m or /d option /s Shutdown /r
Shutdown and Restart /a Abort a system shutdown.
(only during the time-out period)
/p Turn off the local computer with no time-out or warning
(only with /d) /h Hibernate the local computer (only with
/f ) /e Document the reason for an unexpected shutdown of a
computer

options:
/m \\Computer : A remote computer to shutdown.

/t:xxx : Time until system shutdown in seconds.
The valid range is xxx=0-600 seconds. [default=30]
/c "Msg" : An optional shutdown message [Max 127 chars]

/f : Force running applications to close.
This will not prompt for File-Save in any open
applications.
so will result in a loss of all unsaved data!!!

/d u:xx:yy : List a USER reason code for the shutdown.
/d P:xx:yy : List a PLANNED reason code for the shutdown.
xx Specifies the major reason code (0-255)
yy Specifies the minor reason code (0-65536)
0
 

Author Comment

by:AutomaticSlim
ID: 18899763
So how would it look in my Code, would this work: Shell("shutdown.exe -r -f -t 05")
0
 

Author Comment

by:AutomaticSlim
ID: 18899775
and is this a shutdown or restart?
0
 
LVL 15

Expert Comment

by:igor_alpha
ID: 18915973
1. Process.Start("executable.exe", "arguments");
2. /s Shutdown
/r Shutdown and Restart
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

816 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now