-
方便地启动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
<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
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
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,VB.NET Express 2005 Beta 1
出处:https://www.cnblogs.com/daniel_ngn/archive/2004/10/05/49093.html
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式