C# Pause: Using the Pause Function to Suspend a Service

c# pauseC# is a simple, modern, general purpose and object oriented programming language developed by Microsoft. It is component oriented and easy to learn. C# is a structured language used to produce efficient programs. This language is part of the .Net framework and can be compiled on diverse computer platforms. It’s syntax is similar to that of C and C++. C# is specifically designed to be a platform independent language on the lines of Java.

However, unlike C++, multiple inheritance is not supported by C#. Instead interfaces are provided. Interfaces permit several classes to implement the same set of methods. C# is compiled to Microsoft Intermediate Language(MSIL).  MSIIL is similar to Java bytecode and gives C# the capability to be platform independent. It runs using Just In Time(JIT) compilers  and makes it possible for classes to be inherited across languages. Garbage collection is a prominent feature of C#. The language also offers support for direct access to memory. The classic attributes of OOPS such as inheritance, encapsulation and polymorphism are supported by C#. We walk you through the C# pause function in this intermediate level tutorial. We assume you’re familiar with the basics of C#, otherwise you can take this excellent course on C# programming for complete beginners.  (If you’re pressed for time, this amazing course helps you learn C# in under an hour!)

What is the .NET Framework?

.NET Framework is a software framework developed by Microsoft which runs predominantly on Microsoft Windows. It consists of a large library and supports language interoperability across several programming languages. By language interoperability we mean each programming language can use program code created in other programming languages. Note that applications created for .NET framework execute in a software environment known as the Common Language Runtime(CLR). The latter is an application virtual machine offering a host of services which includes exception handling, security and memory management. The CLR and the class library together form the .NET framework.

CLR offers developers a framework that permits applications to execute under multiple computer environments. It is designed with the goal of being a working implementation of Microsoft’s Common Language Infrastructure (CLI). Among other things the Framework Class Library offers numeric algorithms, web program development, network communications, user interface, data access, database connectivity and cryptography.

.NET Framework provides a Common Type System or CTS. All possible datatypes and programming constructs supported by the CLR are defined by the CTS specification. The framework simplifies computer software installation so that it meets the specified security requirements and does not conflict with the previously installed computer software. The Framework Class Library provides classes that encapsulate functions such as graphic rendering, file reading and writing, database operations, XMl document manipulation and more. This is made available to all the languages which use .NET Framework. You can learn more about how C# and the .NET framework with this course.

A Look at a Typical C# Program

A C# program is usually divided into the following parts

  • Namespace declaration
  • A class
  • Class methods
  • Class attributes
  • A Main method
  • Statements & Expressions
  • Comments

Example : C# program to print Good Morning

using System;
namespace GoodMorningApplication
   class GoodMorning
      static void Main(string[] args)
         /* my first program in C# */
         Console.WriteLine("Good Morning");

Let’s look into what the program actually does.

The first line of the program-using keyword includes the System namespace in the program. A C# program usually has multiple using statements The next line gives the namespace declaration. A namespace is nothing but a collection of classes. The GoodMorningApplication namespace contains the class GoodMorning.  The next line gives a class declaration, the class GoodMorning contains the data and method definitions. Classes can contain more than one method.  The GoodMorning class has only one method Main. The next line defines the Main method, which is the entry point for all C# programs. The Main method defines what the class will do when executed. The next line /*…*/ will be ignored by the compiler and is a  comment. WriteLine is a method of the Console class defined in the System namespace. This statement prints “Good Morning” on the screen. The last line Console.ReadKey(); makes the program wait for a key press and it prevents the screen from running and closing quickly when the program is launched.

Keep in mind C# is case sensitive. All statements and expressions in a C# program must end with a semicolon. The C# program execution begins at the Main method. Also unlike Java, the file name can be different from the class name. Learn more about writing your own C# programs with this course.

C# ServiceController.Pause Method

This method suspends a service’s operation. It is present in the Namespace  “System.ServiceProcess”. It throws two exceptions. Win32Exception is thrown when an error occurs while accessing a system API. InvalidOperationException is called when a particular service is not found.

The syntax is as follows:

public void Pause()

The empty parentheses indicate that this function does not accept any arguments. The keyword void indicates that the function returns anything. Note that this function has a public access specifier. In other words everybody can call this function.

The following program shows how to use the Pause method to pause a service. Note that sc_new is an object of the ServiceControllerclass  in the program.

while (sc_new.Status != ServiceControllerStatus.Paused)
Console.WriteLine("Status = " + sc_new.Status);

Let’s walk through this program. sc_new.Pause() suspends a service’s operation. sc_new.Status gets the status of the service that is referenced by this instance. Thread.Sleep(1000) will stop the executing thread for 1000 milliseconds. sc_new.Refresh() refreshes property values by resetting the properties to their current values. Remember that only when the ServiceControllerStatus is Paused can you call Continue for the service. ServiceControllerStatus Enumeration indicates the current state of the service. WriteLine is a method of the Console class defined in the System namespace. It displays the status of the service.

Note that this was just an introduction to C# pause. There’s many more layers to C#. We recommend you explore C# further with this advanced 3 part course (Part I, Part II, Part III), or if you’re more the DIY type, jump over to this course to learn how to create your own Android App in C#.