Oracle i有多个系统服务必须都启动之后才能正常工作但逐个启动比较费事多数学习Oracle但机器又不是太好的朋友也不能容忍每次都自动启动Oracle服务(那样MB的内存在启动时就所剩无几了)所以通常都是要用了再启动这里给出了批量启动windows系统服务的一个例子介绍了操控windows系统服务的技巧
首先新建一个Windows Application工程取名为Oracle Starter
粘贴如下代码文件
FormDesignervb
Partial Public Class Form
Inherits SystemWindowsFormsForm
<SystemDiagnosticsDebuggerNonUserCode()> _
Public Sub New()
MyBaseNew()
This call is required by the Windows Form Designer
InitializeComponent()
End Sub
Form overrides dispose to clean up the component list
<SystemDiagnosticsDebuggerNonUserCode()> _
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing AndAlso components IsNot Nothing Then
componentsDispose()
End If
MyBaseDispose(disposing)
End Sub
Required by the Windows Form Designer
Private components As SystemComponentModelIContainer
NOTE: The following procedure is required by the Windows Form Designer
It can be modified using the Windows Form Designer
Do not modify it using the code editor
<SystemDiagnosticsDebuggerStepThrough()> _
Private Sub InitializeComponent()
MeLabel = New SystemWindowsFormsLabel
MeListBox = New SystemWindowsFormsListBox
MeButton = New SystemWindowsFormsButton
MeButton = New SystemWindowsFormsButton
MeSuspendLayout()
Label
MeLabelAutoSize = True
MeLabelFont = New SystemDrawingFont(Tahoma ! SystemDrawingFontStyleBold SystemDrawingGraphicsUnitPoint CType( Byte))
MeLabelLocation = New SystemDrawingPoint( )
MeLabelName = Label
MeLabelSize = New SystemDrawingSize( )
MeLabelTabIndex =
MeLabelText = Oracle Starter
ListBox
MeListBoxFont = New SystemDrawingFont(Tahoma ! SystemDrawingFontStyleRegular SystemDrawingGraphicsUnitPoint CType( Byte))
MeListBoxFormattingEnabled = True
MeListBoxItemHeight =
MeListBoxLocation = New SystemDrawingPoint( )
MeListBoxName = ListBox
MeListBoxSize = New SystemDrawingSize( )
MeListBoxTabIndex =
Button
MeButtonLocation = New SystemDrawingPoint( )
MeButtonName = Button
MeButtonSize = New SystemDrawingSize( )
MeButtonTabIndex =
MeButtonText = Stop all services
Button
MeButtonLocation = New SystemDrawingPoint( )
MeButtonName = Button
MeButtonSize = New SystemDrawingSize( )
MeButtonTabIndex =
MeButtonText = Start all servies
Form
MeAutoScaleBaseSize = New SystemDrawingSize( )
MeClientSize = New SystemDrawingSize( )
MeControlsAdd(MeButton)
MeControlsAdd(MeButton)
MeControlsAdd(MeListBox)
MeControlsAdd(MeLabel)
MeName = Form
MeText = Oracle Starter
MeResumeLayout(False)
MePerformLayout()
End Sub
Friend WithEvents Label As SystemWindowsFormsLabel
Friend WithEvents ListBox As SystemWindowsFormsListBox
Friend WithEvents Button As SystemWindowsFormsButton
Friend WithEvents Button As SystemWindowsFormsButton
End Class
Formvb
Imports SystemServiceProcess
Imports SystemThreading
Public Class Form
Private procName As String
Private procArray() As String
Private Sub Form_Activated(ByVal sender As Object ByVal e As SystemEventArgs) Handles MeActivated
End Sub
Private Sub Form_Load(ByVal sender As SystemObject ByVal e As SystemEventArgs) Handles MyBaseLoad
procArray() = OracleMTSRecoveryService
procArray() = OracleOraHomeAgent
procArray() = OracleOraHomeTNSListener
procArray() = OracleServiceSFSVDB
procArray() = OracleOraHomeHTTPServer
End Sub
Private Sub Button_Click(ByVal sender As SystemObject ByVal e As SystemEventArgs) Handles ButtonClick
Dim backThread As Thread
Dim i As Int
For i = To
backThread = New Thread(AddressOf procClassStopProc)
backThreadIsBackground = True
procClassprocName = procArray(i)
LabelText = Stopping service: + procClassprocName +
MeRefresh()
backThreadStart()
backThreadJoin()
ListBoxItemsAdd(Service: + procClassprocName + Stopped)
MeRefresh()
backThread = Nothing
Next
LabelText = All services stopped
End Sub
Private Sub Button_Click(ByVal sender As SystemObject ByVal e As SystemEventArgs) Handles ButtonClick
Dim backThread As Thread
Dim i As Int
For i = To
backThread = New Thread(AddressOf procClassStartProc)
procClassprocName = procArray(i)
LabelText = Starting service: + procClassprocName +
MeRefresh()
backThreadStart()
backThreadJoin()
ListBoxItemsAdd(Service: + procClassprocName + Started)
MeRefresh()
backThread = Nothing
Next
LabelText = All services started
End Sub
Public Class procClass
Public Shared procName As String
Public Shared Sub StopProc()
Dim servController As ServiceController = New ServiceController(procName)
If servControllerStatus = ServiceControllerStatusStopped Then
ElseIf servControllerStatus = ServiceControllerStatusPaused Then
servControllerStop()
servControllerWaitForStatus(ServiceControllerStatusStopped)
ElseIf servControllerStatus = ServiceControllerStatusRunning Then
servControllerStop()
servControllerWaitForStatus(ServiceControllerStatusStopped)
End If
End Sub
Public Shared Sub StartProc()
Dim servController As ServiceController = New ServiceController(procName)
If servControllerStatus = ServiceControllerStatusRunning Then
ElseIf servControllerStatus = ServiceControllerStatusPaused Then
servControllerContinue()
servControllerWaitForStatus(ServiceControllerStatusRunning)
ElseIf servControllerStatus = ServiceControllerStatusStopped Then
servControllerStart()
servControllerWaitForStatus(ServiceControllerStatusRunning)
End If
End Sub
End Class
End Class
总结
这个实例只是运用了系统服务控制的基本功能高级功能还不甚了解对于多线程的运用还不是很明确望大家指正
改进
程序在运行时如果焦点离开用户界面便会锁死虽然尝试了多线程仍然不理想应该还Join()方法有关多次修正未果请高手指点谢谢!
测试平台
Windows Server VisualVBNET Beta