DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Nt service and timeSetEvent () API

  1. #1
    Join Date
    Aug 2004
    Posts
    43,023

    Nt service and timeSetEvent () API

    [Originally posted by Jordan]

    Im working with a control that transforme a program into an NT SERVICE.˙ Because its a service, all my code is in modules and class modules.˙ This program is used to monitor a folder and each time a file is put into this folder, my program grab it.˙ So for the main loop i've used the timeSetEvent() API.˙ But when I start my service it popup and error that said : THE INSTRUCTION AT "0X660197F" REFERENCED MEMORY AT "0X6601907F". THE MEMORY COULD NOT BE "READ".

    So I would like to know if someone would know how to correct this problem.

    ˙ ˙ ˙ Thanx

  2. #2
    Join Date
    Aug 2004
    Posts
    43,023

    Re:Nt service and timeSetEvent () API

    [Originally posted by Zip]

    'Module 1
    Private Const BUSINESS_NAME = "RealSoft"
    Private Const MODULE_NAME = "modTimer"

    Public Declare Function SetTimer Lib "user32" _
    ˙ ˙ ˙ ˙ ˙ ˙ (ByVal hwnd As Long, _
    ˙ ˙ ˙ ˙ ˙ ˙ ByVal nIDEvent As Long, _
    ˙ ˙ ˙ ˙ ˙ ˙ ByVal uElapse As Long, _
    ˙ ˙ ˙ ˙ ˙ ˙ ByVal lpTimerFunc As Long) As Long

    Public Declare Function KillTimer Lib "user32" _
    ˙ ˙ ˙ ˙ ˙ ˙ (ByVal hwnd As Long, _
    ˙ ˙ ˙ ˙ ˙ ˙ ByVal nIDEvent As Long) As Long

    Public mlngTimerID As Long
    Public mobjTimer As clsTimer

    Public Sub TimerProc(ByVal hwnd As Long, _
    ˙ ˙ ˙ ˙ ˙ ˙ ˙ ByVal uMsg As Long, _
    ˙ ˙ ˙ ˙ ˙ ˙ ˙ ByVal idEvent As Long, _
    ˙ ˙ ˙ ˙ ˙ ˙ ˙ ByVal dwTime As Long)
    10˙ ˙ On Error GoTo errHandler

    20˙ ˙ ˙ ˙ mobjTimer.TimerFired

    30˙ ˙ ˙ On Error GoTo 0
    40˙ ˙ ˙ Exit Sub

    errHandler:
    50˙ ˙ ˙ ˙ 'ErrorHandler Err, BUSINESS_NAME, MODULE_NAME, "TimerProc", , False, Err.Description & " : Erl=" & Erl
    ˙ ˙
    End Sub

    'clsTimer
    '=======================================================================================
    ' Description˙ : This Class provides a com enabled timer
    '
    '=======================================================================================
    Private Const BUSINESS_NAME = "RealSoft"
    Private Const MODULE_NAME = "clsTimer"

    Private mlngTimerInterval As Long
    Private mblnEnabled As Boolean
    Private mintIntervalCount As Integer
    Private mintInterval As Integer
    Private mbytHeartBeatInterval As Byte

    Public Event OnTimer()
    Public Event OnEnabledChanged(ByVal blnState As Boolean)
    Public Event OnHeartBeat()

    Public Property Get TimerID() As Long
    ˙ ˙ TimerID = mlngTimerID
    End Property

    Public Property Let Enabled(ByVal blnValue As Boolean)
    '==============================================================================
    ' Description:˙ Simply Starts the Timer or Ends The Timer
    '˙ ˙ ˙ ˙ ˙ ˙ ˙ XOR :
    '˙ ˙ ˙ ˙ ˙ ˙ ˙ ˙ ˙ 0 XOR 0 = 0
    '˙ ˙ ˙ ˙ ˙ ˙ ˙ ˙ ˙ 0 XOR 1 = 1 'Changed
    '˙ ˙ ˙ ˙ ˙ ˙ ˙ ˙ ˙ 1 XOR 0 = 1 'Changed
    '˙ ˙ ˙ ˙ ˙ ˙ ˙ ˙ ˙ 1 XOR 1 = 0
    '
    ' Parameters
    '˙ ˙ ˙ (In): True = Start Timer , False = Stop Timer
    '
    '==============================================================================
    ' Changes˙ :
    '˙ ˙ ˙ ˙ ˙ :
    ' Date˙ ˙ ˙ :
    ' By˙ ˙ ˙ ˙ :
    '==============================================================================

    On Error GoTo errHandler
    ˙ ˙ 'If mlngTimerID <> 0 Then
    ˙ ˙ '˙ ˙ Err.Raise 3000, "", "Timer Already Exists " & mlngTimerID
    ˙ ˙ 'End If
    ˙ ˙ If (blnValue Xor mblnEnabled) Then ' Check State has Changed
    ˙ ˙ ˙ ˙ mblnEnabled = blnValue
    ˙ ˙ ˙ ˙ If mblnEnabled = False Then
    ˙ ˙ ˙ ˙ ˙ ˙ 'Kill The Timer
    ˙ ˙ ˙ ˙ ˙ ˙ mlngTimerID = KillTimer(0, mlngTimerID)
    ˙ ˙ ˙ ˙ Else
    ˙ ˙ ˙ ˙ ˙ ˙ 'Start The Timer
    ˙ ˙ ˙ ˙ ˙ ˙ mlngTimerID = SetTimer(0, 0, mlngTimerInterval, AddressOf TimerProc)
    ˙ ˙ ˙ ˙ End If
    ˙ ˙ ˙ ˙ RaiseEvent OnEnabledChanged(mblnEnabled)
    ˙ ˙ End If

    ˙ On Error GoTo 0
    ˙ Exit Property

    errHandler:
    ˙ ˙ ErrorHandler Err, BUSINESS_NAME, MODULE_NAME, "Enabled", , , Err.Description & " : Erl=" & Erl
    ˙ ˙
    End Property

    Public Property Get Enabled() As Boolean
    ˙ ˙ Enabled = mblnEnabled
    End Property

    Public Function getReportRefreshInterval() As Long
    '==============================================================================
    ' Description:
    '
    ' Parameters
    '˙ ˙ ˙ (In):
    '˙ ˙ ˙ (Out):
    '
    ' Returns˙ :
    '
    '==============================================================================
    ' Changes˙ :
    '˙ ˙ ˙ ˙ ˙ :
    ' Date˙ ˙ ˙ :
    ' By˙ ˙ ˙ ˙ :
    '==============================================================================
    On Error GoTo errHandler

    ˙ ˙ getReportRefreshInterval = mlngTimerInterval * mintInterval

    ˙ On Error GoTo 0
    ˙ Exit Function

    errHandler:
    ˙ ˙ ErrorHandler Err, BUSINESS_NAME, MODULE_NAME, "getReportrefreshInterval", , , Err.Description & " : Erl=" & Erl
    ˙ ˙
    End Function

    Public Sub setReportRefreshInterval(ByVal bytHours As Byte, ByVal bytMinuets As Byte, ByVal bytSeconds As Byte)
    ˙ ˙ ˙ '==============================================================================
    ˙ ˙ ˙ ' Description: This routine allows the client to set the interval > 60000
    ˙ ˙ ˙ '
    ˙ ˙ ˙ ' Parameters
    ˙ ˙ ˙ '˙ ˙ ˙ (In): Hours, Mins, Secs
    ˙ ˙ ˙ '
    ˙ ˙ ˙ '==============================================================================
    ˙ ˙ ˙ ' Changes˙ :
    ˙ ˙ ˙ '˙ ˙ ˙ ˙ ˙ :
    ˙ ˙ ˙ ' Date˙ ˙ ˙ :
    ˙ ˙ ˙ ' By˙ ˙ ˙ ˙ :
    ˙ ˙ ˙ '==============================================================================
    ˙ ˙ ˙ ˙ ˙ Dim lngInterval As Long
    ˙ ˙
    10˙ ˙ On Error GoTo errHandler

    20˙ ˙ ˙ ˙ lngInterval = (bytHours * 3600000) + _
    ˙ ˙ ˙ ˙ ˙ ˙ ˙ ˙ ˙ ˙ ˙ ˙ ˙ (bytMinuets * 60000) + _
    ˙ ˙ ˙ ˙ ˙ ˙ ˙ ˙ ˙ ˙ ˙ ˙ ˙ (bytSeconds * 1000&)

    30˙ ˙ ˙ ˙ If lngInterval > 60000 Then '60000 = 1 Min
    40˙ ˙ ˙ ˙ ˙ ˙ TimerInterval = 60000
    50˙ ˙ ˙ ˙ ˙ ˙ mintInterval = lngInterval \ 60000
    60˙ ˙ ˙ ˙ Else
    70˙ ˙ ˙ ˙ ˙ ˙ mintInterval = 1 'Ignore as it is less than 1 Min
    80˙ ˙ ˙ ˙ ˙ ˙ TimerInterval = CInt(lngInterval)
    90˙ ˙ ˙ ˙ End If
    ˙ ˙
    ˙ ˙
    100˙ ˙ ˙ On Error GoTo 0
    110˙ ˙ ˙ Exit Sub

    errHandler:
    120˙ ˙ ˙ 'ErrorHandler Err, BUSINESS_NAME, MODULE_NAME, "ReportRefreshInterval", , True, Err.Description & " : Erl=" & Erl
    ˙ ˙
    End Sub

    Public Property Let TimerInterval(ByVal lngInterval As Long)
    '==============================================================================
    ' Description:˙ This property allows the client to set
    '˙ ˙ ˙ ˙ ˙ ˙ ˙ the timer interval directly; If the client sets this
    '˙ ˙ ˙ ˙ ˙ ˙ ˙ > 60000 then 60000 is used - 1 min
    '
    ' Parameters
    '˙ ˙ ˙ (In):Interval in milliseconds
    '
    '==============================================================================
    ' Changes˙ :
    '˙ ˙ ˙ ˙ ˙ :
    ' Date˙ ˙ ˙ :
    ' By˙ ˙ ˙ ˙ :
    '==============================================================================
    On Error GoTo errHandler

    ˙ ˙ Enabled = False ' The Timer Must Be Stopped
    ˙ ˙
    ˙ ˙ If lngInterval > 60000 Then
    ˙ ˙ ˙ ˙ lngInterval = 60000 'MaxAllowed For the Timer
    ˙ ˙ End If
    ˙ ˙
    ˙ ˙ mintInterval = 1
    ˙ ˙ mlngTimerInterval = lngInterval

    ˙ On Error GoTo 0
    ˙ Exit Property

    errHandler:
    ˙ ˙ 'ErrorHandler Err, BUSINESS_NAME, MODULE_NAME, "TimerInterval", , , Err.Description & " : Erl=" & Erl
    ˙ ˙
    End Property

    Public Property Get TimerInterval() As Long
    ˙ ˙ TimerInterval = mlngTimerInterval
    End Property

    Friend Sub TimerFired()
    10˙ ˙ On Error GoTo errHandler

    20˙ ˙ ˙ ˙ If ((DatePart("n", Time) Mod mbytHeartBeatInterval) = 0) Then
    30˙ ˙ ˙ ˙ ˙ ˙ RaiseEvent OnHeartBeat
    40˙ ˙ ˙ ˙ End If
    ˙ ˙
    50˙ ˙ ˙ ˙ If mblnEnabled Then
    60˙ ˙ ˙ ˙ ˙ ˙ Debug.Print mintIntervalCount, mintInterval
    70˙ ˙ ˙ ˙ ˙ ˙ If mintIntervalCount >= mintInterval Then
    80˙ ˙ ˙ ˙ ˙ ˙ ˙ ˙ mintIntervalCount = 1 'Reset the count
    90˙ ˙ ˙ ˙ ˙ ˙ ˙ ˙ RaiseEvent OnTimer
    100˙ ˙ ˙ ˙ ˙ End If
    110˙ ˙ ˙ ˙ ˙ mintIntervalCount = mintIntervalCount + 1
    120˙ ˙ ˙ End If

    130˙ ˙ ˙ On Error GoTo 0
    140˙ ˙ ˙ Exit Sub

    errHandler:
    150˙ ˙ ˙ 'ErrorHandler Err, BUSINESS_NAME, MODULE_NAME, "TimerFired", , False, Err.Description & " : Erl=" & Erl
    ˙ ˙
    End Sub

    Private Sub Class_Initialize()
    ˙ ˙ Set mobjTimer = Me
    ˙ ˙ mlngTimerInterval = 1000
    ˙ ˙ mintIntervalCount = 1
    ˙ ˙ mbytHeartBeatInterval = 3
    End Sub

    Private Sub Class_Terminate()
    ˙ ˙ Enabled = False
    ˙ ˙ mlngTimerID = KillTimer(0, mlngTimerID) ' Ensuring the Timer Is Dead
    End Sub
    'Create these as a component and then use then in any non form based application .

    Warning this code only works for singletons
    and will cause problems where more than one timer is required

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center