Solved

Windows Service

Posted on 2004-03-26
7
205 Views
Last Modified: 2010-04-15
Am trying to figure out how I would start a process at the following location: "C:\Program Files\Network Associates\VirusScan\mcupdate.exe"

I need to check and see if the process is started.  if not start it, execute, then stop when completed - any help would save my day!!!!
0
Comment
Question by:tbaseflug
7 Comments
 
LVL 12

Expert Comment

by:dfiala13
ID: 10688559
Check out the System.Diagnostics namespace.

Here's a link to the Process class and its Start Method.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdiagnosticsprocessclassstarttopic.asp

And the GetProcesses static method which will tell you what's currently running...

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdiagnosticsprocessclassgetprocessestopic.asp
0
 
LVL 10

Expert Comment

by:eternal_21
ID: 10688854
tbaseflug,

  Go to this page: http://msdn.microsoft.com/vcsharp/downloads/samples/23samples/default.aspx.  You will see an item called "Interacting with a Windows Service in C#".  Download the sample code, and take a look - I think that is more what you are looking for.
0
 

Author Comment

by:tbaseflug
ID: 10689810
eternal_21 -

This does seem closer to what I am looking for - any idea on wether the same concept can applied to a process as opposed to a service - in the ability to start and stop?
0
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

 
LVL 10

Expert Comment

by:eternal_21
ID: 10690529
If you want to start and stop a process, this can all be done through the System.Diagnostics.Process class (as dfiala13 suggested).
0
 
LVL 10

Expert Comment

by:ptmcomp
ID: 10699255
You cannot start or stop a service with those methods. But you can use them to check if it's running. Use the ServiceController to stop and start services.
0
 

Author Comment

by:tbaseflug
ID: 10700049
ptmcomp -

Can you give me a quick code-level example?
0
 
LVL 10

Accepted Solution

by:
eternal_21 earned 500 total points
ID: 10702104
Here is a quick example, taken from the code at the link I provided earlier.  As written it will start the Messenger service if it is not already running or starting.

### C#.NET ###

      const string serviceName = "Messenger";

      ServiceController[] AvailableServices = ServiceController.GetServices();
      foreach(ServiceController AvailableService in AvailableServices)
      {
        if(AvailableService.ServiceName==serviceName)
        {
          switch(AvailableService.Status)
          {
            case ServiceControllerStatus.ContinuePending:
            case ServiceControllerStatus.Running:
            case ServiceControllerStatus.StartPending:
              // Service is already starting
              break;
            default:
              // Service is not running; Start
              AvailableService.Start();
              break;
          }
        }
      }

###

You will need to include a reference in your project to the System.ServiceProcess assembly.  And as the code is written here, you will need a "using System.ServiceProcess" statement.
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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

758 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

19 Experts available now in Live!

Get 1:1 Help Now