Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Smooth Timer (timeSetEvent)

I need a smooth and fast timer for this i done something and used timesetevent api its work fine but the problem is like


'Module Code
Public Const TIME_ONESHOT = 0
Public Const TIME_PERIODIC = 1
Public Const TIME_CALLBACK_EVENT_PULSE = &H20
Public Const TIME_CALLBACK_EVENT_SET = &H10
Public Const TIME_CALLBACK_FUNCTION = &H0
Public Declare Function timeKillEvent Lib "winmm.dll" (ByVal uID As Long) As Long
Public Declare Function timeSetEvent Lib "winmm.dll" (ByVal uDelay As Long, ByVal uResolution As Long, ByVal lpFunction As Long, ByVal dwUser As Long, ByVal uFlags As Long) As Long
Public hMMTimer As Long

'This is the timerproc which will fire on timerevent

Sub TimerProc(ByVal uID As Long, ByVal uMsg As Long, ByVal dwUser As Long, ByVal dw1 As Long, ByVal dw2 As Long)
Form1.Caption = Time
End Sub

''Fome Code
Private Sub Form_Load()
hMMTimer = timeSetEvent(1, 0, AddressOf TimerProc, 0, TIME_PERIODIC Or TIME_CALLBACK_FUNCTION)
End Sub
Private Sub Form_Unload(Cancel As Integer)
timeKillEvent hMMTimer
End Sub





Problem its working fine when im doing 'some calcualtion in timerproc when i m calling another method from timerproc it works in vb enviroment and application crashes in EXE like the above example it will work in vb enviroment and will crash in exe..
and when i just perform some calculation not call any other function from timerproc then it work fine in both exe and vb enviroment.
i need to know why its happening in exe file..
this technique is very good to get smooth and fast timer..
but fails
please help me i need hurry
or is there any other way to get smooth and fast timer other then SetTimer and killtimer apis (its not smooth and fast)

Thank you.
waithing to replay.








Comments

  • BitByBit_ThorBitByBit_Thor Member Posts: 2,444
    : I need a smooth and fast timer for this i done something and used timesetevent api its work fine but the problem is like
    :
    :
    : 'Module Code
    : Public Const TIME_ONESHOT = 0
    : Public Const TIME_PERIODIC = 1
    : Public Const TIME_CALLBACK_EVENT_PULSE = &H20
    : Public Const TIME_CALLBACK_EVENT_SET = &H10
    : Public Const TIME_CALLBACK_FUNCTION = &H0
    : Public Declare Function timeKillEvent Lib "winmm.dll" (ByVal uID As Long) As Long
    : Public Declare Function timeSetEvent Lib "winmm.dll" (ByVal uDelay As Long, ByVal uResolution As Long, ByVal lpFunction As Long, ByVal dwUser As Long, ByVal uFlags As Long) As Long
    : Public hMMTimer As Long
    :
    : 'This is the timerproc which will fire on timerevent
    :
    : Sub TimerProc(ByVal uID As Long, ByVal uMsg As Long, ByVal dwUser As Long, ByVal dw1 As Long, ByVal dw2 As Long)
    : Form1.Caption = Time
    : End Sub
    :
    : ''Fome Code
    : Private Sub Form_Load()
    : hMMTimer = timeSetEvent(1, 0, AddressOf TimerProc, 0, TIME_PERIODIC Or TIME_CALLBACK_FUNCTION)
    : End Sub
    : Private Sub Form_Unload(Cancel As Integer)
    : timeKillEvent hMMTimer
    : End Sub
    :
    :
    :
    :
    :
    : Problem its working fine when im doing 'some calcualtion in timerproc when i m calling another method from timerproc it works in vb enviroment and application crashes in EXE like the above example it will work in vb enviroment and will crash in exe..
    : and when i just perform some calculation not call any other function from timerproc then it work fine in both exe and vb enviroment.
    : i need to know why its happening in exe file..
    : this technique is very good to get smooth and fast timer..
    : but fails
    : please help me i need hurry
    : or is there any other way to get smooth and fast timer other then SetTimer and killtimer apis (its not smooth and fast)
    :
    : Thank you.
    : waithing to replay.
    :

    You do know that you are asking windows to call your function every 1 milisecond do you??? I think you are just using up to many recourses, I think it is keeping windows too busy... so busy that your app does not have time to handle other events! Therefor windows will think your app is hanging and produce an error!

    Greets...
    Richard

  • AsifAliAsifAli Member Posts: 39
    : : I need a smooth and fast timer for this i done something and used timesetevent api its work fine but the problem is like
    : :
    : :
    : : 'Module Code
    : : Public Const TIME_ONESHOT = 0
    : : Public Const TIME_PERIODIC = 1
    : : Public Const TIME_CALLBACK_EVENT_PULSE = &H20
    : : Public Const TIME_CALLBACK_EVENT_SET = &H10
    : : Public Const TIME_CALLBACK_FUNCTION = &H0
    : : Public Declare Function timeKillEvent Lib "winmm.dll" (ByVal uID As Long) As Long
    : : Public Declare Function timeSetEvent Lib "winmm.dll" (ByVal uDelay As Long, ByVal uResolution As Long, ByVal lpFunction As Long, ByVal dwUser As Long, ByVal uFlags As Long) As Long
    : : Public hMMTimer As Long
    : :
    : : 'This is the timerproc which will fire on timerevent
    : :
    : : Sub TimerProc(ByVal uID As Long, ByVal uMsg As Long, ByVal dwUser As Long, ByVal dw1 As Long, ByVal dw2 As Long)
    : : Form1.Caption = Time
    : : End Sub
    : :
    : : ''Fome Code
    : : Private Sub Form_Load()
    : : hMMTimer = timeSetEvent(1, 0, AddressOf TimerProc, 0, TIME_PERIODIC Or TIME_CALLBACK_FUNCTION)
    : : End Sub
    : : Private Sub Form_Unload(Cancel As Integer)
    : : timeKillEvent hMMTimer
    : : End Sub
    : :
    : :
    : :
    : :
    : :
    : : Problem its working fine when im doing 'some calcualtion in timerproc when i m calling another method from timerproc it works in vb enviroment and application crashes in EXE like the above example it will work in vb enviroment and will crash in exe..
    : : and when i just perform some calculation not call any other function from timerproc then it work fine in both exe and vb enviroment.
    : : i need to know why its happening in exe file..
    : : this technique is very good to get smooth and fast timer..
    : : but fails
    : : please help me i need hurry
    : : or is there any other way to get smooth and fast timer other then SetTimer and killtimer apis (its not smooth and fast)
    : :
    : : Thank you.
    : : waithing to replay.
    : :
    :
    : You do know that you are asking windows to call your function every 1 milisecond do you??? I think you are just using up to many recourses, I think it is keeping windows too busy... so busy that your app does not have time to handle other events! Therefor windows will think your app is hanging and produce an error!
    :
    : Greets...
    : Richard
    :
    :




    Then wat is the use of timeSetEvent API??
    and how my application works in VB enviroment??

    Ali's

  • BitByBit_ThorBitByBit_Thor Member Posts: 2,444
    : : : I need a smooth and fast timer for this i done something and used timesetevent api its work fine but the problem is like
    : : :
    : : :
    : : : 'Module Code
    : : : Public Const TIME_ONESHOT = 0
    : : : Public Const TIME_PERIODIC = 1
    : : : Public Const TIME_CALLBACK_EVENT_PULSE = &H20
    : : : Public Const TIME_CALLBACK_EVENT_SET = &H10
    : : : Public Const TIME_CALLBACK_FUNCTION = &H0
    : : : Public Declare Function timeKillEvent Lib "winmm.dll" (ByVal uID As Long) As Long
    : : : Public Declare Function timeSetEvent Lib "winmm.dll" (ByVal uDelay As Long, ByVal uResolution As Long, ByVal lpFunction As Long, ByVal dwUser As Long, ByVal uFlags As Long) As Long
    : : : Public hMMTimer As Long
    : : :
    : : : 'This is the timerproc which will fire on timerevent
    : : :
    : : : Sub TimerProc(ByVal uID As Long, ByVal uMsg As Long, ByVal dwUser As Long, ByVal dw1 As Long, ByVal dw2 As Long)
    : : : Form1.Caption = Time
    : : : End Sub
    : : :
    : : : ''Fome Code
    : : : Private Sub Form_Load()
    : : : hMMTimer = timeSetEvent(1, 0, AddressOf TimerProc, 0, TIME_PERIODIC Or TIME_CALLBACK_FUNCTION)
    : : : End Sub
    : : : Private Sub Form_Unload(Cancel As Integer)
    : : : timeKillEvent hMMTimer
    : : : End Sub
    : : :
    : : :
    : : :
    : : :
    : : :
    : : : Problem its working fine when im doing 'some calcualtion in timerproc when i m calling another method from timerproc it works in vb enviroment and application crashes in EXE like the above example it will work in vb enviroment and will crash in exe..
    : : : and when i just perform some calculation not call any other function from timerproc then it work fine in both exe and vb enviroment.
    : : : i need to know why its happening in exe file..
    : : : this technique is very good to get smooth and fast timer..
    : : : but fails
    : : : please help me i need hurry
    : : : or is there any other way to get smooth and fast timer other then SetTimer and killtimer apis (its not smooth and fast)
    : : :
    : : : Thank you.
    : : : waithing to replay.
    : : :
    : :
    : : You do know that you are asking windows to call your function every 1 milisecond do you??? I think you are just using up to many recourses, I think it is keeping windows too busy... so busy that your app does not have time to handle other events! Therefor windows will think your app is hanging and produce an error!
    : :
    : : Greets...
    : : Richard
    : :
    : :
    :
    :
    :
    :
    : Then wat is the use of timeSetEvent API??
    : and how my application works in VB enviroment??
    :
    : Ali's
    :
    :

    I think you should set the interval time a little bit bigger... like 50 ms! Windows isn't a real time OS!


    Greets...
    Richard

  • AsifAliAsifAli Member Posts: 39
    : : : : I need a smooth and fast timer for this i done something and used timesetevent api its work fine but the problem is like
    : : : :
    : : : :
    : : : : 'Module Code
    : : : : Public Const TIME_ONESHOT = 0
    : : : : Public Const TIME_PERIODIC = 1
    : : : : Public Const TIME_CALLBACK_EVENT_PULSE = &H20
    : : : : Public Const TIME_CALLBACK_EVENT_SET = &H10
    : : : : Public Const TIME_CALLBACK_FUNCTION = &H0
    : : : : Public Declare Function timeKillEvent Lib "winmm.dll" (ByVal uID As Long) As Long
    : : : : Public Declare Function timeSetEvent Lib "winmm.dll" (ByVal uDelay As Long, ByVal uResolution As Long, ByVal lpFunction As Long, ByVal dwUser As Long, ByVal uFlags As Long) As Long
    : : : : Public hMMTimer As Long
    : : : :
    : : : : 'This is the timerproc which will fire on timerevent
    : : : :
    : : : : Sub TimerProc(ByVal uID As Long, ByVal uMsg As Long, ByVal dwUser As Long, ByVal dw1 As Long, ByVal dw2 As Long)
    : : : : Form1.Caption = Time
    : : : : End Sub
    : : : :
    : : : : ''Fome Code
    : : : : Private Sub Form_Load()
    : : : : hMMTimer = timeSetEvent(1, 0, AddressOf TimerProc, 0, TIME_PERIODIC Or TIME_CALLBACK_FUNCTION)
    : : : : End Sub
    : : : : Private Sub Form_Unload(Cancel As Integer)
    : : : : timeKillEvent hMMTimer
    : : : : End Sub
    : : : :
    : : : :
    : : : :
    : : : :
    : : : :
    : : : : Problem its working fine when im doing 'some calcualtion in timerproc when i m calling another method from timerproc it works in vb enviroment and application crashes in EXE like the above example it will work in vb enviroment and will crash in exe..
    : : : : and when i just perform some calculation not call any other function from timerproc then it work fine in both exe and vb enviroment.
    : : : : i need to know why its happening in exe file..
    : : : : this technique is very good to get smooth and fast timer..
    : : : : but fails
    : : : : please help me i need hurry
    : : : : or is there any other way to get smooth and fast timer other then SetTimer and killtimer apis (its not smooth and fast)
    : : : :
    : : : : Thank you.
    : : : : waithing to replay.
    : : : :
    : : :
    : : : You do know that you are asking windows to call your function every 1 milisecond do you??? I think you are just using up to many recourses, I think it is keeping windows too busy... so busy that your app does not have time to handle other events! Therefor windows will think your app is hanging and produce an error!
    : : :
    : : : Greets...
    : : : Richard
    : : :
    : : :
    : :
    : :
    : :
    : :
    : : Then wat is the use of timeSetEvent API??
    : : and how my application works in VB enviroment??
    : :
    : : Ali's
    : :
    : :
    :
    : I think you should set the interval time a little bit bigger... like 50 ms! Windows isn't a real time OS!
    :
    :
    : Greets...
    : Richard
    :
    :
    I used 100
    Interval is not the problem its working fine when i m using 1 the problem is only that its not working in EXE when i m calling other function through timerproc then its crashed other wise its work fine in both cases.

    Ali's

  • BitByBit_ThorBitByBit_Thor Member Posts: 2,444
    : : : : : I need a smooth and fast timer for this i done something and used timesetevent api its work fine but the problem is like
    : : : : :
    : : : : :
    : : : : : 'Module Code
    : : : : : Public Const TIME_ONESHOT = 0
    : : : : : Public Const TIME_PERIODIC = 1
    : : : : : Public Const TIME_CALLBACK_EVENT_PULSE = &H20
    : : : : : Public Const TIME_CALLBACK_EVENT_SET = &H10
    : : : : : Public Const TIME_CALLBACK_FUNCTION = &H0
    : : : : : Public Declare Function timeKillEvent Lib "winmm.dll" (ByVal uID As Long) As Long
    : : : : : Public Declare Function timeSetEvent Lib "winmm.dll" (ByVal uDelay As Long, ByVal uResolution As Long, ByVal lpFunction As Long, ByVal dwUser As Long, ByVal uFlags As Long) As Long
    : : : : : Public hMMTimer As Long
    : : : : :
    : : : : : 'This is the timerproc which will fire on timerevent
    : : : : :
    : : : : : Sub TimerProc(ByVal uID As Long, ByVal uMsg As Long, ByVal dwUser As Long, ByVal dw1 As Long, ByVal dw2 As Long)
    : : : : : Form1.Caption = Time
    : : : : : End Sub
    : : : : :
    : : : : : ''Fome Code
    : : : : : Private Sub Form_Load()
    : : : : : hMMTimer = timeSetEvent(1, 0, AddressOf TimerProc, 0, TIME_PERIODIC Or TIME_CALLBACK_FUNCTION)
    : : : : : End Sub
    : : : : : Private Sub Form_Unload(Cancel As Integer)
    : : : : : timeKillEvent hMMTimer
    : : : : : End Sub
    : : : : :
    : : : : :
    : : : : :
    : : : : :
    : : : : :
    : : : : : Problem its working fine when im doing 'some calcualtion in timerproc when i m calling another method from timerproc it works in vb enviroment and application crashes in EXE like the above example it will work in vb enviroment and will crash in exe..
    : : : : : and when i just perform some calculation not call any other function from timerproc then it work fine in both exe and vb enviroment.
    : : : : : i need to know why its happening in exe file..
    : : : : : this technique is very good to get smooth and fast timer..
    : : : : : but fails
    : : : : : please help me i need hurry
    : : : : : or is there any other way to get smooth and fast timer other then SetTimer and killtimer apis (its not smooth and fast)
    : : : : :
    : : : : : Thank you.
    : : : : : waithing to replay.
    : : : : :
    : : : :
    : : : : You do know that you are asking windows to call your function every 1 milisecond do you??? I think you are just using up to many recourses, I think it is keeping windows too busy... so busy that your app does not have time to handle other events! Therefor windows will think your app is hanging and produce an error!
    : : : :
    : : : : Greets...
    : : : : Richard
    : : : :
    : : : :
    : : :
    : : :
    : : :
    : : :
    : : : Then wat is the use of timeSetEvent API??
    : : : and how my application works in VB enviroment??
    : : :
    : : : Ali's
    : : :
    : : :
    : :
    : : I think you should set the interval time a little bit bigger... like 50 ms! Windows isn't a real time OS!
    : :
    : :
    : : Greets...
    : : Richard
    : :
    : :
    : I used 100
    : Interval is not the problem its working fine when i m using 1 the problem is only that its not working in EXE when i m calling other function through timerproc then its crashed other wise its work fine in both cases.
    :
    : Ali's
    :

    Well, i don't know it then. It might have something to do with the fact that it is a CallBack function... But here we are talking C/C++!


    Greets...
    Richard

  • Raven737Raven737 Member Posts: 3
    Hi, i had the same problem. I needed to repeatedly and reliably execute code in a 1ms interval(high resolution timer). timeSetEvent is apparently a good way to do that.

    The problem is, as far as i understand, that the callback is executed on a thread that does not belong strictly to the application. If you try to access any memory (variables) of you application then you get an error. In the IDE this works because Visual Studio is doing some background magic that is needed for debugging purposes which allows such cross boundary memory access. In any case, one solution is to call PostMessage api function (without referencing app variables) to post a message to your apps message queue and then process that.

    The better solution for me was to simply create a event and pass the handle to that. Then in a thread wait for that event to be set and then invoke your OnEvent method or simply put your code there. This allowed me to reliably execute code in a 1ms interval. See example below:

    [code]Public Const EVENT_LOOP = 1
    Public Const TIME_CALLBACK_EVENT_SET = &H10
    _
    Private Function timeSetEvent(ByVal uDelay As Integer, ByVal uResolution As Integer, ByVal lpFunction As IntPtr, ByRef dwUser As Integer, ByVal uFlags As Integer) As Integer
    End Function

    Private oSync As New Threading.EventWaitHandle(False,
    Private Sub EventThread()
    Do
    oSync.WaitOne()
    Call OnEvent()
    Loop
    End Sub
    Threading.EventResetMode.AutoReset)1
    Sub Main()
    Dim iUser As Integer = 0
    Call Threading.ThreadPool.QueueUserWorkItem(AddressOf EventThread)
    Call timeSetEvent(1, 1, oSync.Handle, iUser, EVENT_LOOP Or TIME_CALLBACK_EVENT_SET)
    Console.ReadKey()
    End Sub[/code]
  • Raven737Raven737 Member Posts: 3
    Hi, i had the same problem. I needed to repeatedly and reliably execute code in a 1ms interval(high resolution timer). timeSetEvent is apparently a good way to do that.

    The problem is, as far as i understand, that the callback is executed on a thread that does not belong strictly to the application. If you try to access any memory (variables) of you application then you get an error. In the IDE this works because Visual Studio is doing some background magic that is needed for debugging pusposes which allows such cross boundary memory access. In any case, one solution is to call PostMessage api function (without referencing app variables) to post a message to you apps message queue and then process that.

    The better solution for me was to simply create a event and pass the handle to that. Then in a thread wait for that event to be set and then invoke you OnEvent method. This allowed me to reliably execute code in a 1ms interval. See example below:

    [code]Public Const EVENT_LOOP = 1
    Public Const TIME_CALLBACK_EVENT_SET = &H10
    _
    Private Function timeSetEvent(ByVal uDelay As Integer, ByVal uResolution As Integer, ByVal lpFunction As IntPtr, ByRef dwUser As Integer, ByVal uFlags As Integer) As Integer
    End Function

    Private Sub EventThread()
    Do
    oSync.WaitOne()
    Call OnEvent()
    Loop
    End Sub
    Private oSync As New Threading.EventWaitHandle(False, Threading.EventResetMode.AutoReset)
    Sub Main()
    Call Threading.ThreadPool.QueueUserWorkItem(AddressOf SendThread)
    Call timeSetEvent(1, 1, oSync.Handle, iUser, EVENT_LOOP Or TIME_CALLBACK_EVENT_SET)
    Console.ReadKey()
    End Sub[/code]
  • Raven737Raven737 Member Posts: 3
    Accidental report
Sign In or Register to comment.