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
Solved

C# to End WINWORD.EXE task in task manager

Posted on 2007-04-10
8
5,544 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!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JSON  parse help 7 40
Dynamics CRM Plugin Query TotalRecordCount returning -1 13 43
asp.net mvc , views, hidden values ? 2 16
Visual Studio npm 1 11
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

856 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