Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
If Me.WindowState = FormWindowState.Minimized Then
NotifyIcon1.Visible = true
Me.Hide()
NotifyIcon1.BalloonTipText = "Hi from right system tray"
NotifyIcon1.ShowBalloonTip(500)
End If
End Sub
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.
SaveLog(i & ". statrted at " & DateTime.Now.ToString, "C:\LOG.txt")
AddHandler timer.Elapsed, AddressOf OnElapsedTime
timer.Interval = 3600000
timer.Enabled = True
End Sub
Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your service.
SaveLog(i & ". stoped at " & DateTime.Now.ToString, "C:\LOG.txt")
End Sub
Public Sub SaveLog(ByVal strData As String, ByVal FullPath As String)
Try
Dim fs As New FileStream("C:\LOG.txt", FileMode.OpenOrCreate, FileAccess.Write)
Dim sw As New StreamWriter(fs)
sw.BaseStream.Seek(0, SeekOrigin.End)
sw.WriteLine(strData)
sw.Flush()
sw.Close()
Catch Ex As Exception
End Try
End Sub
Protected Sub OnElapsedTime(ByVal source As Object, ByVal e As ElapsedEventArgs)
i += 1
SaveLog(i & ". processing at " & DateTime.Now.ToString, "C:\LOG.txt")
End Sub
คือสั่งให้ทำงาน พร้อมวินโดว์ครับแล้ว เขียนลง log ไฟล์ ทุกๆ 1 ชั่วโมงครับ
ไม่แน่ใจว่าที่ผมทำนี่ถูกวัตถุประสงค์ของ windows service หรือป่าวครับ
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.
SaveLog(i & ". statrted at " & DateTime.Now.ToString, "C:\LOG.txt")
AddHandler timer.Elapsed, AddressOf OnElapsedTime
timer.Interval = 3600000
timer.Enabled = True
showBalloonTip(SystemIcons.Application, "CNWservice is running.", System.Windows.Forms.ToolTipIcon.Info)
End Sub
Code (VB.NET)
Private Sub showBalloonTip(icon As Icon, TipText As String, TipIcon As System.Windows.Forms.ToolTipIcon)
Dim notifyicon As System.Windows.Forms.NotifyIcon = New System.Windows.Forms.NotifyIcon
notifyicon.Icon = icon
notifyicon.BalloonTipTitle = "CNWservice"
notifyicon.BalloonTipText = TipText
notifyicon.BalloonTipIcon = TipIcon
notifyicon.Visible = True
notifyicon.ShowBalloonTip(3000)
End Sub