How do I access services remotely and efficiently in C#? Exception handling is very slow

1

I'm writing a program that displays the status of specific services on a number of remote computers. Currently, I'm using ServiceController to get this data and it is working fine when there is adequate permissions.

However, when the user doesn't have access to the remote computer, the program slows down dramatically. For example, while populating a ListView with the required data on initialization, the program will take about a minute to throw and catch 5 System.InvalidOperationException.

I'm guessing that it's just taking a while on the network but I'm torn in terms of how to work through this issue. It seems that on this site, some advocate for designing around the use of exception handling for performance reasons, while others state that it's important to catch exceptions while doing the actual file i/o in case anything changes between the check and the i/o.

My gut is that exception handling is the way to go here because otherwise I'm looking at checking for permissions, which seems unadvisable. (see: Checking for directory and file write permissions in .NET). Does anyone know a way that I can work through this performance issue?

Below is the code that is slowing down the program. Much thanks for any advice on this!

  ServiceController sc = new ServiceController();
  sc.MachineName = line;
  sc.ServiceName = "VRaySpawner 2016";

  try
  {
       if (sc.Status.Equals(ServiceControllerStatus.Running))
           entry[1] = "ON";                  

        sc.ServiceName = "BACKBURNER_SRV_200";
        if (sc.Status.Equals(ServiceControllerStatus.Running))
           entry[2] = "ON";

   }
   catch (InvalidOperationException e)
   {
         entry[1] = "Access Denied";                        
         Debug.WriteLine("Exception Caught");
   }

Update 1 based on comments ServiceController is being used to start and stop services on the remote machines. To do so, it draws the remote machines' names from a listview with:

 ServiceController sc = new ServiceController();
 sc.ServiceName = "BACKBURNER_SRV_200";

foreach (ListViewItem item in listView1.SelectedItems) 
{ 
       String strItem = item.SubItems[0].Text; 
       sc.MachineName = strItem; 
       if (sc.Status.Equals(ServiceControllerStatus.Stopped) || 
            sc.Status.Equals(ServiceControllerStatus.StopPending))
               sc.Start(); 
}

The stack trace for when the exception is thrown:

System.InvalidOperationException
  HResult=0x80131509
  Message=Cannot open VRaySpawner 2016 service on computer 'dto302322'.
  Source=System.ServiceProcess
  StackTrace:
   at System.ServiceProcess.ServiceController.GetServiceHandle(Int32 desiredAccess)
   at System.ServiceProcess.ServiceController.GenerateStatus()
   at System.ServiceProcess.ServiceController.get_Status()
   at RenderFarmDemo.Form1.refreshList() in C:\Apps\RenderFarmDemo\RenderFarmDemo\Form2.cs:line 219
   at RenderFarmDemo.Form1.button5_Click(Object sender, EventArgs e) in C:\Apps\RenderFarmDemo\RenderFarmDemo\Form2.cs:line 207
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)
   at RenderFarmDemo.Program.Main() in C:\Apps\RenderFarmDemo\RenderFarmDemo\Program.cs:line 19

Inner Exception 1:
Win32Exception: Access is denied 
c#
performance
exception-handling
remote-access
asked on Stack Overflow May 29, 2018 by drewh • edited May 29, 2018 by drewh

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0