方便地啟動Oracle服務(VB.NET 2005 Windows服務操控與多線程)

來源:互聯網
上載者:User
oracle|window|多線程 Oracle 9i有多個系統服務必須都啟動之後才能正常工作,但逐個啟動比較費事,多數學習Oracle但機器又不是太好的朋友也不能容忍每次都自動啟動Oracle服務(那樣512MB的記憶體在啟動時就所剩無幾了),所以通常都是要用了再啟動,這裡給出了大量啟動windows系統服務的一個例子,介紹了操控windows系統服務的技巧:

首先建立一個Windows Application工程,取名為Oracle Starter
粘貼如下代碼檔案:
Form1.Designer.vb



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

<System.Diagnostics.DebuggerNonUserCode()> _
Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

End Sub

'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'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.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.Label1 = New System.Windows.Forms.Label
Me.ListBox1 = New System.Windows.Forms.ListBox
Me.Button1 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Font = New System.Drawing.Font("Tahoma", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label1.Location = New System.Drawing.Point(13, 13)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(122, 22)
Me.Label1.TabIndex = 0
Me.Label1.Text = "Oracle Starter"
'
'ListBox1
'
Me.ListBox1.Font = New System.Drawing.Font("Tahoma", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.ListBox1.FormattingEnabled = True
Me.ListBox1.ItemHeight = 19
Me.ListBox1.Location = New System.Drawing.Point(13, 75)
Me.ListBox1.Name = "ListBox1"
Me.ListBox1.Size = New System.Drawing.Size(436, 175)
Me.ListBox1.TabIndex = 1
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(322, 257)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(127, 34)
Me.Button1.TabIndex = 2
Me.Button1.Text = "Stop all services"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(188, 258)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(127, 34)
Me.Button2.TabIndex = 3
Me.Button2.Text = "Start all servies"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(461, 304)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.ListBox1)
Me.Controls.Add(Me.Label1)
Me.Name = "Form1"
Me.Text = "Oracle Starter"
Me.ResumeLayout(False)
Me.PerformLayout()

End Sub
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button

End Class



--------------------------------------------------------------------------------

Form1.vb

--------------------------------------------------------------------------------

Imports System.ServiceProcess
Imports System.Threading

Public Class Form1

'Private procName As String
Private procArray(5) As String

Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
procArray(0) = "OracleMTSRecoveryService"
procArray(1) = "OracleOraHome92Agent"
procArray(2) = "OracleOraHome92TNSListener"
procArray(3) = "OracleServiceSFSVDB"
procArray(4) = "OracleOraHome92HTTPServer"
End Sub


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim backThread As Thread
Dim i As Int16
For i = 0 To 4
backThread = New Thread(AddressOf procClass.StopProc)
backThread.IsBackground = True
procClass.procName = procArray(i)
Label1.Text = "Stopping service: " + procClass.procName + " ..."
Me.Refresh()
backThread.Start()
backThread.Join()
ListBox1.Items.Add("Service: " + procClass.procName + " Stopped")
Me.Refresh()
backThread = Nothing
Next
Label1.Text = "All services stopped"
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim backThread As Thread
Dim i As Int16
For i = 0 To 4
backThread = New Thread(AddressOf procClass.StartProc)
procClass.procName = procArray(i)
Label1.Text = "Starting service: " + procClass.procName + " ..."
Me.Refresh()
backThread.Start()
backThread.Join()
ListBox1.Items.Add("Service: " + procClass.procName + " Started")
Me.Refresh()
backThread = Nothing
Next
Label1.Text = "All services started"
End Sub

Public Class procClass
Public Shared procName As String

Public Shared Sub StopProc()
Dim servController1 As ServiceController = New ServiceController(procName)
If servController1.Status = ServiceControllerStatus.Stopped Then
ElseIf servController1.Status = ServiceControllerStatus.Paused Then
servController1.Stop()
servController1.WaitForStatus(ServiceControllerStatus.Stopped)
ElseIf servController1.Status = ServiceControllerStatus.Running Then
servController1.Stop()
servController1.WaitForStatus(ServiceControllerStatus.Stopped)
End If
End Sub

Public Shared Sub StartProc()
Dim servController1 As ServiceController = New ServiceController(procName)
If servController1.Status = ServiceControllerStatus.Running Then
ElseIf servController1.Status = ServiceControllerStatus.Paused Then
servController1.Continue()
servController1.WaitForStatus(ServiceControllerStatus.Running)
ElseIf servController1.Status = ServiceControllerStatus.Stopped Then
servController1.Start()
servController1.WaitForStatus(ServiceControllerStatus.Running)
End If
End Sub
End Class
End Class

總結:
這個執行個體只是運用了系統服務控制的準系統,進階功能還不甚瞭解,對於多線程的運用還不是很明確,望大家指正

改進:
程式在運行時如果焦點離開,使用者介面便會鎖死,雖然嘗試了多線程,仍然不理想,應該還Join()方法有關,多次修正未果,請高手指點,謝謝!

測試平台:
Windows Server 2003,Visual VB.NET 2005 Beta 1



相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。