Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Exploring WMI: Integrate WMI into the .NET Framework : Page 4

Once you learn how to integrate Windows Management Instrumentation (WMI) with the .NET Framework, you can utilize a completely new approach for managing system resources.


advertisement
Testing Time
You can test the application now. It should display a list of all installed services, and the toolbar buttons should be enabled accordingly to the state and features of the selected service.

What's still missing is the action that should take place when the user presses one of your toolbar buttons. Luckily, you have everything prepared: your ServiceNode class is able to call the InvokeMethod method on your management object. But how do you know when a service finishes an invoked operation? Starting and stopping services takes some time. You can't permanently refresh your ListViewItem until you get a different state from the object. The solution would be to use WMI Events, but these are far beyond the scope of this article. So stick with a simple solution, and wait a few seconds before you update the status of your item:

private void ServiceToolBar_ButtonClick(object sender,
System.Windows.Forms.ToolBarButtonClickEventArgs e) { Cursor = Cursors.WaitCursor; ServiceNode sn = (ServiceNode)lvServices.SelectedItems[0]; switch((string)e.Button.Tag) { case "Resume": sn.ChangeState(ServiceNode.ServiceState.Resume); break; case "Pause": sn.ChangeState(ServiceNode.ServiceState.Pause); break; case "Start": sn.ChangeState(ServiceNode.ServiceState.Start); break; case "Stop": sn.ChangeState(ServiceNode.ServiceState.Stop); break; case "Restart": sn.ChangeState(ServiceNode.ServiceState.Stop); sn.ChangeState(ServiceNode.ServiceState.Start); break; } System.Threading.Thread.Sleep(5000); sn.Refresh(); lvServices_SelectedIndexChanged(this, new System.EventArgs()); Cursor = Cursors.Default; }



To show the user that your program is doing some important, time-consuming work, change the Cursor on your form to WaitCursor. You get the reference to the selected service into the nice, short named variable sn. Since you stored the state string for the services in the tag of your toolbar buttons, you can use it to decide which action should be performed on the selected service. After calling the ChangeState method, which in turn calls InvokeMethod on the management object, you need to wait a few seconds for the service to finish its work. You do this with a call on System.Threading.Thread.Sleep. Yes, this is a quick and dirty hack, but a better solution is beyond the scope of this article.

When the sleeping time of your thread is over, refresh your service node. To get the toolbar buttons updated, you simply call the event handler for SelectedIndexChanged. You have already implemented the logic for your toolbar buttons there, so no need to type that code sequence twice. Now that your work unit is finished, you can put the cursor back to its default state.

Running the Windows Service Manager
Now you can build and run the application. You should get a list of installed services just like in the first screenshot of your application. Try to start and stop a service, and use the service applet from the management console to control the success of your actions.

Author Note: Be careful when stopping or pausing services. Some of them may be vital to your system. If you shut a vital service down, you can cause damage to your system. Try the program with some unimportant services like the index service or the alerter service.

What Have You Learned?
This article introduced the concepts of WMI and addressed the following questions:

  • Why do you need something like WMI?
  • How does it represent the WBEM Standard?
  • Who is responsible for this standard?

After the theoretical introduction to WBEM and the concepts developed by the DMTF, it examined the System.Management Namespace and the classes it contains. Special attention was put on the ManagementBaseObject, ManagementObject, ManagementClass, and the ManagementObjectCollection, since these are the classes needed for the first sample program.

The sample program is a Windows Service Manager, similar to the management console applet for Windows services. Included features are listing all services (including name, description, status, and startup type) and the manipulation of services (start, stop, restart, pause, and continue).

Limitations and Further Work
As stated previously, simply waiting five seconds and then updating the service status isn't a clean solution. You should use WMI events to make this happen cleanly and asynchronously. A nice feature that you could add would be a remote connection to another machine or the ability to specify the user account that should be used to impersonate on WMI.



Peter Koen is an independent consultant, author, and programmer. He is certified as a MCP, MCAD, MCDBA, and MCT in Microsoft technologies, and has received a Microsoft MVP award for Visual C#. .
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date