Results 1 to 3 of 3

Thread: Write interactive code "Print Spooler" in C# ?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Posts
    481

    Write interactive code "Print Spooler" in C# ?

    In windows 7, my computer often fails to print, I have to go to Control Panel->Administrative Tools->Services->Print spooler to switch from "Stop" to "Start" mode. In C# winform I want to write code to do the above instead of going to ...Services->Print spooler ? How do I write code ?

  2. #2
    Hyperactive Member
    Join Date
    Jun 2019
    Posts
    339

    Re: Write interactive code "Print Spooler" in C# ?

    Execute:
    Code:
    net stop spooler
    and then
    Code:
    net start spooler

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,461

    Re: Write interactive code "Print Spooler" in C# ?

    I just ran this code and it worked:
    csharp Code:
    1. using System;
    2. using System.ServiceProcess;
    3. using System.Threading;
    4.  
    5. public class Program
    6. {
    7.     static void Main()
    8.     {
    9.         using var printSpoolerController = new ServiceController("Print Spooler");
    10.  
    11.         if (printSpoolerController.Status == ServiceControllerStatus.Stopped)
    12.         {
    13.             printSpoolerController.Start();
    14.  
    15.             for (var i = 0; i < 10; i++)
    16.             {
    17.                 printSpoolerController.Refresh();
    18.  
    19.                 if (printSpoolerController.Status == ServiceControllerStatus.Running)
    20.                 {
    21.                     Console.WriteLine(i);
    22.                     break;
    23.                 }
    24.  
    25.                 Thread.Sleep(100);
    26.             }
    27.         }
    28.  
    29.         Console.WriteLine(printSpoolerController.Status);
    30.     }
    31. }
    In my testing, it only took one Sleep call for the status of the service to change.

    Note that I had to run the app as an admin or an exception was thrown.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width