Restart the print spooler in C#


Every now and then my computer gets confused and thinks the printer is offline. That wouldn’t be so bad except Windows doesn’t provide a simple way to bring the printer back online. Basically you need to shutdown and restart the print spooler. I wrote this example to make that easier.

When you click the Restart button, the program executes the following code.

// Add a reference to System.ServiceProcess.
using System.ServiceProcess;
private void btnRestart_Click(object sender, EventArgs e)
    Cursor = Cursors.WaitCursor;

    // Stop the spooler.
    ServiceController service = new ServiceController("Spooler");
    if ((!service.Status.Equals(ServiceControllerStatus.Stopped)) &&
        lblStatus.Text = "Stopping spooler...";


    // Start the spooler.
    lblStatus.Text = "Restarting spooler...";

    lblStatus.Text = "Done";
    Cursor = Cursors.Default;

The program uses the System.ServiceProcess.ServiceController class so the code makes that easier by starting with a using System.ServiceProcess directive. To use that namespace, add System.ServiceProcess to the project’s references.

When you click the button, the code creates a new ServiceController for the service named “Spooler.” If the spooler isn’t currently stopped or pending a stop, the code calls its Stop method and then waits until the spooler stops.

Now that the spooler is stopped, the code calls its Start method and waits until it is finished starting.

This is pretty easy but there is a catch. You need to run the program with sufficient privileges to stop and restart the spooler. One way to do that is to right-click on the program’s executable and select “Run as administrator.”

Download Example   Follow me on Twitter   RSS feed   Donate

About RodStephens

Rod Stephens is a software consultant and author who has written more than 30 books and 250 magazine articles covering C#, Visual Basic, Visual Basic for Applications, Delphi, and Java.
This entry was posted in printing, system and tagged , , , , , , , , , , . Bookmark the permalink.

8 Responses to Restart the print spooler in C#

  1. Marco de Haart says:

    Hello, I was searching for this solution because the printer causes sometimes errors that are not reseted.

    Using Windows 10 on a 64 bit machine.

    First I get an message in the object: SC.ServiceHandle = ‘SC.ServiceHandle’ threw an exception of type ‘System.InvalidOperationException’

    If I execute SC.Stop() I get:
    an exception : 2147467259
    message: {“Toegang geweigerd”} (translated) “Access denied”

    Any suggestion.

  2. Koy says:

    Thank a lot for your code and explanation. Really appreciate.

  3. lasantha herath says:

    This code line worked successfully. to get the administrator privilege you need to do this..
    In Visual Studio, right click project -> Add Item -> Choose Application Manifest File ( under General for Visual C# items)
    In manifest file update the following code segments

  4. Damian Jones says:

    Hi Rod,
    I tried running your app and get this error:
    {“Cannot open Spooler service on computer ‘.’.”}

    any ideas?

    • RodStephens says:

      My only guess is that you’re not running it with administrator privileges. Look at the last paragraph of the post.

      Try building the executable. Then right-click the exe file and select “Run as administrator.” that should give you permission to shutdown and restart the spooler.

  5. Srinivasa Rao Palakaluri says:

    Thank you so much 🙂

  6. David says:

    Very helpful – many thanks!!!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.