Change from WindowsForm to windows service...
Hello to all,
I hope that you could help me,
I create a windowsForm with just two buttons one is Start and other is Stop (to simulate the start and stop of my windows service).
The project is running fine, now i want to change to Windows Service, i think that is possibel i hope so :).
How can i do it?
This is the code that i have in my windowsForm to start running the project.
Thanks for your time.
VB Code:
Imports SifoxPM.MainConnection
Imports System.Threading
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
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.
Friend WithEvents btn_start As System.Windows.Forms.Button
Friend WithEvents btn_stop As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.btn_start = New System.Windows.Forms.Button
Me.btn_stop = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'btn_start
'
Me.btn_start.Location = New System.Drawing.Point(102, 54)
Me.btn_start.Name = "btn_start"
Me.btn_start.Size = New System.Drawing.Size(88, 32)
Me.btn_start.TabIndex = 0
Me.btn_start.Text = "Start"
'
'btn_stop
'
Me.btn_stop.Location = New System.Drawing.Point(102, 110)
Me.btn_stop.Name = "btn_stop"
Me.btn_stop.Size = New System.Drawing.Size(88, 32)
Me.btn_stop.TabIndex = 1
Me.btn_stop.Text = "Stop"
Me.btn_stop.Enabled = False
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 271)
Me.Controls.Add(Me.btn_stop)
Me.Controls.Add(Me.btn_start)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Dim csection As New CriticalSection(Nothing, Nothing, Nothing)
Private Sub btn_start_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_start.Click
Dim mainThread As New Thread(AddressOf csection.MainWorkingThread)
mainThread.Start()
btn_start.Enabled = False
btn_stop.Enabled = True
End Sub
Private Sub btn_stop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_stop.Click
btn_stop.Enabled = False
csection.EndService()
End Sub
End Class
Re: Change from WindowsForm to windows service...
Well you first need to create a new WIndows Service Project, it is a particular project type. In the service, there is an OnStart() and OnStop() sub where you put the initializing code and stopping code of the service, along with any other code you wish to do. Note that you will not see a form in the designer, just a box that allows you to add only components to it, as services tend not to have a graphical interface.