?
Solved

C# to End WINWORD.EXE task in task manager

Posted on 2007-04-10
8
Medium Priority
?
5,670 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 

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

CHALLENGE LAB: Troubleshooting Connectivity Issues

Goal: Fix the connectivity issue in the lab's AWS environment so that you can SSH into the provided EC2 instance.  

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

762 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