473,387 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Windows service operation throws error: Operation is not valid due to the current state of the object.

I have this code, which uses WMI to operate on Windows service from C#
code.

When Service.InvokeMethod is called, the method throws an exception:
System.Management Operation is not valid due to the current state of
the object. at System.Management.ManagementObject.InvokeMethod(St ring
methodName, Object[] args)
Here is the method.

RequestStatus is Enum type defined in the class.

ServerName - Machine where the service might be running

ServiceName - Service name on which the operation is to be performed

OpInstructions - Operation to be performed ( StartService,
StopService )

I have tried Start, StartService, RestartService, Stop, StopService.
None have worked.

Services I tried these actions on: Alerter, Messenger, MSSQLSERVER.

Any ideas whats going wrong? Thanks.

public static RequestStatus OperateService ( string ServerName, string
ServiceName, string OpInstructions )

{

ConnectionOptions Options = new
ConnectionOptions();

Options.EnablePrivileges = true;

//Service name space: \root\cimv2

ManagementScope Scope = new
ManagementScope( @"\\" + ServerName + @"\root\cimv2", Options );

Scope.Connect();

if ( Scope.IsConnected )

{

ObjectQuery Query = new
ObjectQuery( "SELECT State FROM Win32_Service WHERE Name = '" +
ServiceName + "'" );
ManagementObjectCollection Services = new ManagementObjectSearcher (
Scope, Query ).Get();

if ( Services.Count > 0
)

{

foreach (
ManagementObject Service in Services )

{
Service.InvokeMethod( OpInstructions, null );

}

return
RequestStatus.Yes;

}

else

{

return
RequestStatus.NotFound;

}
}

return RequestStatus.No;

}

Nov 17 '05 #1
1 6950
Found the problem. My WQL query was wrong. It needed Name.

SELECT State, Name FROM Win32_Service WHERE Name = '" +
ServiceName + "'"

Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ken Lindner | last post by:
I have a need to become familiar with SQL Server 2000 for work. Needless to say I am new to SQL Server any version, but not IT in general. My employer has provided me with the SQL Server 2000...
5
by: Dhilip Kumar | last post by:
Hi All, I'm writing a Windows Service app using C#. I need to read some configuration settings before the service starts up. These settings will be used by the service in its operation. ...
3
by: THNQDigital | last post by:
Hi All, I have written VB.NET Windows Service whcih reads some information from Outlook and creates an Excel report outa it. When i make a console application and run the program everything...
4
by: Groundskeeper | last post by:
I can't seem to get a custom UnhandledException handler working for a Windows Service I'm writing in VB.NET. I've read the MSDN and tried various different placements of the AddHandler call, to no...
0
by: N! Xau | last post by:
Ciao, Hi, a datagrid on my form displays a table T, fine. On the form I have a button. I want to execute a query on T, just clicking that button. DB is Oracle. Button_Click code is: .......
8
by: Jim in Arizona | last post by:
I've been using an example out of a book to be able to edit the rows in a database. I am getting the following error: ========================================================...
0
by: whizpop | last post by:
Hi, First of all, thanks for a great starter kit, now If I could just get it to work (fully). I am trying to compile and run the solution/services all on a local dev box. I am able to...
11
by: Joseph Geretz | last post by:
I've been looking at two approaches for the maintenance of Session state for a Web Service application. One approach uses the old familiar Session object which I've used in the past for Web...
0
by: dom_perron | last post by:
Hi, I have a problem when I try to start an application using MSMQ from a Windows Service. The application runs on a server. The client send a message to a public queue created on the server,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.