方便地启动Oracle服务(VB.NET 2005 Windows服务操控与多线程)

Oracle 9i有多个系统服务必须都启动之后才能正常工作,但逐个启动比较费事,多数学习Oracle但机器又不是太好的朋友也不能容忍每次都自动启动Oracle服务(那样512MB的内存在启动时就所剩无几了),所以通常都是要用了再启动,这里给出了批量启动windows系统服务的一个例子,介绍了操控windows系统服务的技巧:

首先新建一个Windows Application工程,取名为Oracle Starter
粘贴如下代码文件:
Form1.Designer.vb


Partial Public Class Form1
Inherits System.Windows.Forms.Form

  1<system.diagnostics.debuggernonusercode()> _   
  2Public Sub New()   
  3MyBase.New() 
  4
  5'This call is required by the Windows Form Designer.   
  6InitializeComponent() 
  7
  8End Sub 
  9
 10'Form overrides dispose to clean up the component list.   
 11<system.diagnostics.debuggernonusercode()> _   
 12Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)   
 13If disposing AndAlso components IsNot Nothing Then   
 14components.Dispose()   
 15End If   
 16MyBase.Dispose(disposing)   
 17End Sub 
 18
 19'Required by the Windows Form Designer   
 20Private components As System.ComponentModel.IContainer 
 21
 22'NOTE: The following procedure is required by the Windows Form Designer   
 23'It can be modified using the Windows Form Designer.   
 24'Do not modify it using the code editor.   
 25<system.diagnostics.debuggerstepthrough()> _   
 26Private Sub InitializeComponent()   
 27Me.Label1 = New System.Windows.Forms.Label   
 28Me.ListBox1 = New System.Windows.Forms.ListBox   
 29Me.Button1 = New System.Windows.Forms.Button   
 30Me.Button2 = New System.Windows.Forms.Button   
 31Me.SuspendLayout()   
 32'   
 33'Label1   
 34'   
 35Me.Label1.AutoSize = True   
 36Me.Label1.Font = New System.Drawing.Font("Tahoma", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))   
 37Me.Label1.Location = New System.Drawing.Point(13, 13)   
 38Me.Label1.Name = "Label1"   
 39Me.Label1.Size = New System.Drawing.Size(122, 22)   
 40Me.Label1.TabIndex = 0   
 41Me.Label1.Text = "Oracle Starter"   
 42'   
 43'ListBox1   
 44'   
 45Me.ListBox1.Font = New System.Drawing.Font("Tahoma", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))   
 46Me.ListBox1.FormattingEnabled = True   
 47Me.ListBox1.ItemHeight = 19   
 48Me.ListBox1.Location = New System.Drawing.Point(13, 75)   
 49Me.ListBox1.Name = "ListBox1"   
 50Me.ListBox1.Size = New System.Drawing.Size(436, 175)   
 51Me.ListBox1.TabIndex = 1   
 52'   
 53'Button1   
 54'   
 55Me.Button1.Location = New System.Drawing.Point(322, 257)   
 56Me.Button1.Name = "Button1"   
 57Me.Button1.Size = New System.Drawing.Size(127, 34)   
 58Me.Button1.TabIndex = 2   
 59Me.Button1.Text = "Stop all services"   
 60'   
 61'Button2   
 62'   
 63Me.Button2.Location = New System.Drawing.Point(188, 258)   
 64Me.Button2.Name = "Button2"   
 65Me.Button2.Size = New System.Drawing.Size(127, 34)   
 66Me.Button2.TabIndex = 3   
 67Me.Button2.Text = "Start all servies"   
 68'   
 69'Form1   
 70'   
 71Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)   
 72Me.ClientSize = New System.Drawing.Size(461, 304)   
 73Me.Controls.Add(Me.Button2)   
 74Me.Controls.Add(Me.Button1)   
 75Me.Controls.Add(Me.ListBox1)   
 76Me.Controls.Add(Me.Label1)   
 77Me.Name = "Form1"   
 78Me.Text = "Oracle Starter"   
 79Me.ResumeLayout(False)   
 80Me.PerformLayout() 
 81
 82End Sub   
 83Friend WithEvents Label1 As System.Windows.Forms.Label   
 84Friend WithEvents ListBox1 As System.Windows.Forms.ListBox   
 85Friend WithEvents Button1 As System.Windows.Forms.Button   
 86Friend WithEvents Button2 As System.Windows.Forms.Button 
 87
 88End Class   
 89
 90
 91* * *
 92
 93  
 94**Form1.vb**   
 95
 96
 97* * *
 98
 99  
100Imports System.ServiceProcess   
101Imports System.Threading 
102
103Public Class Form1 
104
105'Private procName As String   
106Private procArray(5) As String 
107
108Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated 
109
110End Sub 
111
112Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load   
113procArray(0) = "OracleMTSRecoveryService"   
114procArray(1) = "OracleOraHome92Agent"   
115procArray(2) = "OracleOraHome92TNSListener"   
116procArray(3) = "OracleServiceSFSVDB"   
117procArray(4) = "OracleOraHome92HTTPServer"   
118End Sub 
119
120  
121Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click   
122Dim backThread As Thread   
123Dim i As Int16   
124For i = 0 To 4   
125backThread = New Thread(AddressOf procClass.StopProc)   
126backThread.IsBackground = True   
127procClass.procName = procArray(i)   
128Label1.Text = "Stopping service: " + procClass.procName + " ..."   
129Me.Refresh()   
130backThread.Start()   
131backThread.Join()   
132ListBox1.Items.Add("Service: " + procClass.procName + " Stopped")   
133Me.Refresh()   
134backThread = Nothing   
135Next   
136Label1.Text = "All services stopped"   
137End Sub 
138
139Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click   
140Dim backThread As Thread   
141Dim i As Int16   
142For i = 0 To 4   
143backThread = New Thread(AddressOf procClass.StartProc)   
144procClass.procName = procArray(i)   
145Label1.Text = "Starting service: " + procClass.procName + " ..."   
146Me.Refresh()   
147backThread.Start()   
148backThread.Join()   
149ListBox1.Items.Add("Service: " + procClass.procName + " Started")   
150Me.Refresh()   
151backThread = Nothing   
152Next   
153Label1.Text = "All services started"   
154End Sub 
155
156Public Class procClass   
157Public Shared procName As String 
158
159Public Shared Sub StopProc()   
160Dim servController1 As ServiceController = New ServiceController(procName)   
161If servController1.Status = ServiceControllerStatus.Stopped Then   
162ElseIf servController1.Status = ServiceControllerStatus.Paused Then   
163servController1.Stop()   
164servController1.WaitForStatus(ServiceControllerStatus.Stopped)   
165ElseIf servController1.Status = ServiceControllerStatus.Running Then   
166servController1.Stop()   
167servController1.WaitForStatus(ServiceControllerStatus.Stopped)   
168End If   
169End Sub 
170
171Public Shared Sub StartProc()   
172Dim servController1 As ServiceController = New ServiceController(procName)   
173If servController1.Status = ServiceControllerStatus.Running Then   
174ElseIf servController1.Status = ServiceControllerStatus.Paused Then   
175servController1.Continue()   
176servController1.WaitForStatus(ServiceControllerStatus.Running)   
177ElseIf servController1.Status = ServiceControllerStatus.Stopped Then   
178servController1.Start()   
179servController1.WaitForStatus(ServiceControllerStatus.Running)   
180End If   
181End Sub   
182End Class   
183End Class   
184  
185总结:   
186这个实例只是运用了系统服务控制的基本功能,高级功能还不甚了解,对于多线程的运用还不是很明确,望大家指正   
187  
188改进:   
189程序在运行时如果焦点离开,用户界面便会锁死,虽然尝试了多线程,仍然不理想,应该还Join()方法有关,多次修正未果,请高手指点,谢谢!   
190  
191测试平台:   
192Windows Server 2003,Visual VB.NET 2005 Beta 1</system.diagnostics.debuggerstepthrough()></system.diagnostics.debuggernonusercode()></system.diagnostics.debuggernonusercode()>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus