Easy to start Oracle Services (vb.net) Windows services manipulation and multithreading

Source: Internet
Author: User
Tags join thread
oracle|window| multi-threaded Oracle 9i There are multiple system services that must be started before it works, but it's a lot of trouble to start one at a Most of the friends who learn Oracle but not the machine are too good to be able to start the Oracle service automatically every time (so that 512MB of RAM is running out of the box), so it's usually a restart, and here's an example of a batch-start Windows system service. Describes the techniques for manipulating Windows system services:

First, create a new Windows Application project named Oracle Starter
Paste the following code file:
Form1.Designer.vb



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

<system.diagnostics.debuggernonusercode () > _
Public Sub New ()
MyBase.New ()

' This are 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

Summarize:
This example only uses the system service control basic function, the advanced function is not very understanding, for the use of multithreading is not very clear, hope everybody correct me

Improved:
When the program is running, if the focus is left, the user interface will be locked, although the attempt of multithreading, still not ideal, should also join () method related, many amendments have not been, please expert advice, thank you!

Test Platform:
Windows Server 2003,visual vb.net 1



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.