方便地启动Oracle服务(VB.NET 2005 Windows服务操控与多线程)
2024-07-10 13:05:22
供稿:网友
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