[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

C# to End WINWORD.EXE task in task manager

Posted on 2007-04-10
8
Medium Priority
?
5,786 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 1000 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month19 days, 21 hours left to enroll

873 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