Note that there are some explanatory texts on larger screens.

plurals
  1. POServiceControler.GetServices() silently bailing in a background thread
    text
    copied!<p>I am running the following line to get a list of all services for a given computer:</p> <pre><code>ServiceController[] services = ServiceController.GetServices(compName); </code></pre> <p>If I run this on the <strong>main thread</strong> and pass in a computer name that exists but I don't have permissions to view the services for, such as: </p> <pre><code>ServiceController.GetServices("OtherComp"); </code></pre> <blockquote> <p>InvalidOperationException:<br> Cannot open Service Control Manager on computer 'OtherComp'. This operation might require other privileges.</p> </blockquote> <p>I fully expect this to happen. However my issue comes with running this on a <strong>background thread</strong>. Take this fully complete console program:</p> <pre><code>using System.ComponentModel; using System.ServiceProcess; namespace ServiceTesting { internal class Program { private static void Main(string[] args) { ServiceAccessor sa = new ServiceAccessor(); sa.Run(); } } public class ServiceAccessor { BackgroundWorker bw; public ServiceAccessor() { bw = new BackgroundWorker(); bw.DoWork += new DoWorkEventHandler(bw_DoWork); bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted); } public void Run() { bw.RunWorkerAsync(); } private void bw_DoWork(object sender, DoWorkEventArgs e) { //this line is where the bail happens var services = ServiceController.GetServices("OtherComputer"); } void bw_RunWorkerCompleted(object sender,RunWorkerCompletedEventArgs e) { // the code will never get to this method if (e.Error != null) { //something } } } } </code></pre> <p><strong>I would expect an exception to be thrown, but as soon as the code tries to execute this line, it bails out of the thread.</strong></p> <p>I can't wrap the line in a <code>try\catch</code>; it will won't catch it. This might be simmilar to <code>ThreadAbort</code> problems with asp.net (but that is just a guess).</p> <p>The <a href="http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicecontroller%28v=vs.90%29.ASPX" rel="nofollow">msdn page on the ServiceController Class</a> says that the static function is thread safe, however, a commenter on the <a href="http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicecontroller.getservices%28v=vs.90%29.aspx" rel="nofollow">function's msdn page</a> says that it is not.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload