方便地啟動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