Solved

C# to End WINWORD.EXE task in task manager

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

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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

747 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

11 Experts available now in Live!

Get 1:1 Help Now