Click here to Skip to main content
15,910,471 members
Home / Discussions / Visual Basic
   

Visual Basic

 
AnswerRe: Windows Services Problem? Pin
Dave Kreskowiak25-Jun-04 3:47
mveDave Kreskowiak25-Jun-04 3:47 
GeneralRe: Windows Services Problem? Pin
mythinky27-Jun-04 15:24
mythinky27-Jun-04 15:24 
GeneralRe: Windows Services Problem? Pin
Dave Kreskowiak27-Jun-04 18:13
mveDave Kreskowiak27-Jun-04 18:13 
GeneralRe: Windows Services Problem? Pin
Anonymous27-Jun-04 23:36
Anonymous27-Jun-04 23:36 
GeneralRe: Windows Services Problem? Pin
Dave Kreskowiak28-Jun-04 0:41
mveDave Kreskowiak28-Jun-04 0:41 
GeneralRe: Windows Services Problem? Pin
mythinky28-Jun-04 15:49
mythinky28-Jun-04 15:49 
GeneralRe: Windows Services Problem? Pin
Dave Kreskowiak28-Jun-04 16:48
mveDave Kreskowiak28-Jun-04 16:48 
QuestionEmail in Windows Service? Pin
mythinky24-Jun-04 18:05
mythinky24-Jun-04 18:05 
Helo, I have build a windows service that send email automatically.
Problem comes when we start the service, it will stop directly since it has nothing to do.
How can we start the service then it runs for long time, it shouldn't stop the service if have nothing to do, because i need to check every time.

I have attached the code for email windows service. Please review it.


Imports System.ComponentModel
Imports System.ServiceProcess
Imports System.Configuration.Install

Imports System.Data
Imports System.Data.OleDb
Imports System.DBNull
Imports System.Runtime.InteropServices
Imports System.Net

Public Class LibraryEmail
Inherits System.ServiceProcess.ServiceBase

Public Sub New()
MyBase.New()

' This call is required by the Component Designer.
InitializeComponent()

' Add any initialization after the InitializeComponent() call

End Sub

Shared Sub Main()
Dim ServicesToRun() As System.ServiceProcess.ServiceBase

' More than one NT Service may run within the same process. To add
' another service to this process, change the following line to
' create a second service object. For example,
'
' ServicesToRun = New System.ServiceProcess.ServiceBase () {New UserService1, New MySecondUserService}
'
ServicesToRun = New System.ServiceProcess.ServiceBase() {New LibraryEmail()}

System.ServiceProcess.ServiceBase.Run(ServicesToRun)
End Sub

#Region " Component Designer generated code "



'Required by the Component Designer
Private components As System.ComponentModel.IContainer

' NOTE: The following procedure is required by the Component Designer
' It can be modified using the Component Designer.
' Do not modify it using the code editor.
Friend WithEvents Timer1 As System.Timers.Timer
'<system.diagnostics.debuggerstepthrough()>
Friend WithEvents ServiceController1 As System.ServiceProcess.ServiceController
Friend WithEvents Process1 As System.Diagnostics.Process
Friend WithEvents Timer2 As System.Timers.Timer
Private Sub InitializeComponent()
Me.Timer1 = New System.Timers.Timer()
Me.ServiceController1 = New System.ServiceProcess.ServiceController()
Me.Process1 = New System.Diagnostics.Process()
Me.Timer2 = New System.Timers.Timer()
CType(Me.Timer1, System.ComponentModel.ISupportInitialize).BeginInit()
CType(Me.Timer2, System.ComponentModel.ISupportInitialize).BeginInit()
'
'Timer1
'
'
'Process1
'
'
'Timer2
'
Me.Timer2.Enabled = True
'
'LibraryEmail
'
Me.ServiceName = "LibraryEmail"
CType(Me.Timer1, System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.Timer2, System.ComponentModel.ISupportInitialize).EndInit()

End Sub

#End Region

Public sConnectDBLib As String
Public sConnectDBSchool As String
Public sDBLib, sDBSch As String

Public objConnect As OleDbConnection

<dllimport("wininet.dll")> _
Public Shared Function InternetGetConnectedState(ByRef description As Integer, ByVal reservedValue As Integer) As Boolean
End Function

Public Shared Function IsConnectedToInternet() As Boolean
Dim desc As Integer
Return InternetGetConnectedState(desc, 0)
End Function


Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set things
' in motion so your service can do its work.
Timer1.Enabled = True
Dim sServer, sUsername, sPassword As String
Dim bConnect As Boolean
Dim i As Integer

sServer = GetSetting("Library", "DBConn", "server", "")
sDBLib = GetSetting("Library", "DBConn", "dblib", "")
sUsername = GetSetting("Library", "DBConn", "username", "")
sPassword = GetSetting("Library", "DBConn", "password", "")

sConnectDBLib = "Provider=SQLOLEDB; Data Source = " & sServer & _
";Initial catalog=" & sDBLib & "; User ID=" & sUsername & _
"; Password=" & sPassword & ";"
For i = 1 To 3
If Not CheckConnection(sConnectDBLib) Then
MsgBox("Check the database setting first")
Else
bConnect = True
End If

If bConnect Then
objConnect = Nothing
Exit For
Else
If i <> 3 Then
MsgBox("Connection Failed! Please try again!")
Else
MsgBox("Connection Failed! Fail to run the program!")
End If
End If
Next i
End Sub

Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your service.
End Sub

Function CheckConnection(ByVal sConnDBLib As String) As Boolean
Dim bConn As Boolean
Dim oConnDBLib As OleDbConnection = New OleDbConnection(sConnDBLib)

Try
oConnDBLib.Open()

bConn = True
Catch err As Exception
bConn = False
End Try
oConnDBLib = Nothing
Return bConn
End Function

Private Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed
Dim objConLib As OleDb.OleDbConnection = New OleDb.OleDbConnection(sConnectDBLib)
Dim objDataAdp As OleDb.OleDbDataAdapter
Dim objDataSet As DataSet

' To see if the date has changed
'If DateDiff(DateInterval.Day, dteLastDate, Date.Now) > 0 Then
' MsgBox("Today is now yesterday")
'End If
' To see if it's a certain time

'If Date.Now.ToShortTimeString = "9:50 PM" Then
Dim Before, After As Integer
Dim DDay As Boolean

Before = Val(GetSetting("Library", "Setting", "Before", "3"))
After = Val(GetSetting("Library", "Setting", "After", "3"))
DDay = CBool(GetSetting("Library", "Setting", "DueDate", "1"))

Dim countBefore, countToday, countAfter As Integer
Dim countBeforeMsg, countTodayMsg, countAfterMsg As Integer
objDataAdp = New OleDb.OleDbDataAdapter()

Dim SMTPServer As String
objDataAdp.SelectCommand = New OleDbCommand("SELECT * FROM SETTING", objConLib)
objDataAdp.SelectCommand.CommandType = CommandType.Text
objDataAdp.Fill(objDataSet, "SMTP")
If objDataSet.Tables("SMTP").Rows.Count > 0 Then
SMTPServer = objDataSet.Tables("SMTP").Rows(0).ItemArray(2).ToString
Else
MsgBox("Message can't be send due to SMTP server name. Please check it first.")
Exit Sub
End If

'select all loan where 3 days before due date
objDataAdp.SelectCommand = New OleDb.OleDbCommand("SELECT M.EMAIL FROM MEMBER M LEFT JOIN " & _
"USERS A ON M.MEMBERID= A.ID LEFT JOIN LOANITEM LI ON LI.LOANID=M.LOANID WHERE " & _
"LI.DATERETURNED IS NULL AND LI.DUEDATE = '" & FormatDateTime(DateAdd(DateInterval.Day, Before, Now), DateFormat.ShortDate) & "'", objConLib)
objDataAdp.SelectCommand.CommandType = CommandType.Text
objDataSet = New DataSet()
objDataAdp.Fill(objDataSet, "BEFOREOVERDUEEMAIL")


countBefore = objDataSet.Tables("BEFOREOVERDUEEMAIL").Rows.Count

Dim toEmail, subject, body As String
Dim i As Integer

If countBefore = 0 Then
exit sub
Else
Try
For i = 0 To countBefore - 1
toEmail = objDataSet.Tables("BEFOREOVERDUEEMAIL").Rows(i).Item("Type").ToString & ";"
Next

Dim newEmail As System.Web.Mail.MailMessage
newEmail = New System.Web.Mail.MailMessage()

With newEmail
.To = toEmail
.From = "librarian@wefgroup.com"

.Subject = "helo"
.Body = "test"

End With

System.Web.Mail.SmtpMail.SmtpServer = SMTPServer
If IsConnectedToInternet() Then
Try
' Try and resolve the address of the mail server.
Dim hostEntry As IPHostEntry = Dns.GetHostByName(SMTPServer)
' If we get here, then the DNS Resolve worked and
'we must be connected to the 'Net.
Try
System.Web.Mail.SmtpMail.Send(newEmail)
MsgBox("Email sent!!!")
Catch ex As Exception
' Send message failed for some reason...
MsgBox(ex.InnerException.InnerException.Message)
End Try
Catch ex As Exception
' DNS Resolve failed. We can't send the message...
MsgBox("Message can't be send due to SMTP server. Please check it first.")
End Try
Else
MsgBox("Please check the internet connection")
End If
Catch exc As Exception
MsgBox(exc.Message)
End Try
end if
End Class

<runinstallerattribute(true)> _
Public Class ProjectInstaller
Inherits Installer

Private serviceInstaller As serviceInstaller
Private processInstaller As ServiceProcessInstaller
Private otherServiceInstaller As serviceInstaller

Sub New()

processInstaller = New ServiceProcessInstaller()
serviceInstaller = New ServiceInstaller()

processInstaller.Account = ServiceAccount.LocalSystem
serviceInstaller.StartType = ServiceStartMode.Automatic
serviceInstaller.ServiceName = "LibraryEmail"

Installers.Add(serviceInstaller)
Installers.Add(processInstaller)

End Sub

End Class


Learning .NET
AnswerRe: Email in Windows Service? Pin
Dave Kreskowiak25-Jun-04 3:36
mveDave Kreskowiak25-Jun-04 3:36 
GeneralRe: Email in Windows Service? Pin
mythinky27-Jun-04 15:37
mythinky27-Jun-04 15:37 
GeneralRe: Email in Windows Service? Pin
mythinky27-Jun-04 15:49
mythinky27-Jun-04 15:49 
GeneralRe: Email in Windows Service? Pin
Dave Kreskowiak27-Jun-04 18:29
mveDave Kreskowiak27-Jun-04 18:29 
GeneralRe: Email in Windows Service? Pin
Dave Kreskowiak27-Jun-04 18:21
mveDave Kreskowiak27-Jun-04 18:21 
GeneralSave As Type Pin
Esther C24-Jun-04 17:29
Esther C24-Jun-04 17:29 
GeneralRe: Save As Type Pin
Esther C24-Jun-04 17:41
Esther C24-Jun-04 17:41 
GeneralRe: Save As Type Pin
Dave Kreskowiak25-Jun-04 3:00
mveDave Kreskowiak25-Jun-04 3:00 
Generalsystem tray Pin
Anonymous24-Jun-04 16:14
Anonymous24-Jun-04 16:14 
GeneralRe: system tray Pin
Dave Kreskowiak25-Jun-04 2:42
mveDave Kreskowiak25-Jun-04 2:42 
QuestionHow to Select Root in TreeView? Pin
vancouver77724-Jun-04 12:44
vancouver77724-Jun-04 12:44 
AnswerRe: How to Select Root in TreeView? Pin
Dave Kreskowiak25-Jun-04 4:23
mveDave Kreskowiak25-Jun-04 4:23 
GeneralRe: How to Select Root in TreeView? Pin
vancouver77725-Jun-04 6:28
vancouver77725-Jun-04 6:28 
GeneralRe: How to Select Root in TreeView? Pin
Dave Kreskowiak25-Jun-04 7:17
mveDave Kreskowiak25-Jun-04 7:17 
AnswerRe: How to Select Root in TreeView? Pin
Ravi S.V.29-Jun-04 1:03
Ravi S.V.29-Jun-04 1:03 
General. Pin
hssaroch24-Jun-04 1:09
hssaroch24-Jun-04 1:09 
GeneralRe: Reading XL files in VB6 Pin
RichardGrimmer24-Jun-04 6:11
RichardGrimmer24-Jun-04 6:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.