paul_1966

Hi

I still got problems using a windows.forms.timer.

I decleare my timer as follows:

Code Snippet

Friend WithEvents Timer1 As System.Windows.Forms.Timer

The sub i have connected to the timer event looks like this:

Code Snippet

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

Timer1.Enabled = False 'Paul Turn off timer

' tmrRead: call tmrRead on every occasion of timer event.

If TmrReadE = True Then tmrReadSub()

' tmrStartUpSequence: call tmrStartUpSequence approx every 0.9 sec.

If (tmrCounter Mod 3) = 0 And TmrStartupSequenceE = True Then

tmrStartupSequenceSub()

End If

' SecTimer: call SecTimer sub approx every 0.9 sec.

If (tmrCounter Mod 3) = 0 And SecTimerE = True Then

SecTimerSub()

End If

' Timer1: call timer1 sub approx every 1.8 sec.

If (tmrCounter Mod 6) = 0 And Timer1E = True Then

timer1Sub()

End If

'Finally count up counter

If tmrCounter = 29999 Then

tmrCounter = 0

Else

tmrCounter = tmrCounter + 1

End If

Timer1.Enabled = True 'Turn on timer

End Sub

I disable the timer when entering the sub.

Then i call the subroutines:

- tmrReadSub()

- tmrStartupSequenceSub()

- SecTimerSub()

- timer1Sub()

Finally i enable the timer again.

At some occasions i can see the following in my "call stack" window:

Code Snippet

> WindowsApplication1.UserInterface.Timer1_Tick(Object sender = {System.Windows.Forms.Timer}, System.EventArgs e = {System.EventArgs}) Line 4524 Basic
System.Windows.Forms.Timer.OnTick
System.Windows.Forms.Timer.DebuggableCallback
WindowsApplication1.PLC.ConnectToPlc() Line 266 Basic
WindowsApplication1.SystemInit.FirstInit(Boolean status = True) Line 405 Basic
WindowsApplication1.UserInterface.tmrStartupSequenceSub() Line 4562 Basic
WindowsApplication1.UserInterface.Timer1_Tick(Object sender = {System.Windows.Forms.Timer}, System.EventArgs e = {System.EventArgs}) Line 4519 Basic
System.Windows.Forms.Timer.OnTick
System.Windows.Forms.Timer.DebuggableCallback
System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods+IMsoComponentManager.FPushMessageLoop
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner
System.Windows.Forms.Application.ThreadContext.RunMessageLoop
System.Windows.Forms.Application.Run
WindowsApplication1.UserInterface.Main() Line 13 Basic

I have two calls of Timer1_Tick on my stack!

By clicking on the Timer1_Tick in the "call stack" then i can see that my variables contain different values for the two calls on the stack.

Why is this

In one of my subroutines (tmrStartupSequenceSub) i perform some "system calls" such as

- InputBox(...)

- AppendFile(...)

- And some other method calls in other modules.

My assumption was that i could call these subroutines without any problems as i have disabled my timer.

However i seems to get calls to my timer sub anyway.

Will these sub calls interfear with my system in such way that i will get calls to my timer sub even if

my timer is disabled at the moment

If there is a problem using such system calls, how shall i then prevent from getting those stacked timer calls

Regards Paul

(beginner in VB)

I




Re: Visual Basic Language Timer problems.

Spidermans_DarkSide - VSIP

Hi Paul,

Here is a very simple timer example, just add a Timer and two buttons to your Form,

watch the FORM title text update with the date and the time from Now every second.

I hope you find it a useful example.

Code Snippet

Public Class Form1

' Set the interval to 1000 milliseconds = 1 second

' within the Timer control.

' ENABLE the timer.

Private Sub Form1_Load _

(ByVal sender As System.Object, _

ByVal e As System.EventArgs) _

Handles MyBase.Load

Timer1.Interval = 1000

Timer1.Enabled = True

End Sub

Private Sub Timer1_Tick _

(ByVal sender As System.Object, _

ByVal e As System.EventArgs) _

Handles Timer1.Tick

' Show the date and time in the FORM title bar!!

Me.Text = Now.ToLongDateString & " " & Now.ToLongTimeString

End Sub

Private Sub Button1_Click _

(ByVal sender As System.Object, _

ByVal e As System.EventArgs) _

Handles Button1.Click

'Start the timer.

Timer1.Start()

End Sub

Private Sub Button2_Click _

(ByVal sender As System.Object, _

ByVal e As System.EventArgs) _

Handles Button2.Click

'Stop the timer.

Timer1.Stop()

End Sub

End Class

Regards,

S_DS






Re: Visual Basic Language Timer problems.

spotty

S_DS solution works fine but relies on the timer being placed ont he form at design time. You can create the object at runtime and it will work just fine


Code Snippet

Public Class Form1

Dim WithEvents timer1 As New Timer

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Me.timer1.Interval = 1000

Me.timer1.Start()

End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timer1.Tick

' Show the date and time in the FORM title bar!!

Me.Text = Now.ToLongDateString & " " & Now.ToLongTimeString

End Sub

End Class

Thats said, your description doesnt really describe what your doing from a high level perspective. You tell use that you stop the timer then call the subroutines:

- tmrReadSub()

- tmrStartupSequenceSub()

- SecTimerSub()

- timer1Sub()


What are these doing - and what happens if an exception occurs you dont have any exception handling code so the timer wouldnt start again.

What is the purpose you are trying to achieve with this timer - then we may be able to suggest a better way to write you code so it will work as expected.





Re: Visual Basic Language Timer problems.

Spidermans_DarkSide - VSIP

Hi spotty,

Any idea what the default timer interval is

Regards,

S_DS






Re: Visual Basic Language Timer problems.

spotty

100 ms would be the default as far as I can tell - so in a real world application you would want to set this to a realistic level.

In the original example - no interval is set in code shown and that would cause a huge amount of events to be created, which could definately cause stack issues eventually.

From the above code it would look like the interval is maybe set to 300 as mod 3 and a description every 0.9 sec. But thats an assumption. If that would be the case then why not set the interval to 900 and respond on every tick or for sub called every 1.8 second - every 2nd tick.





Re: Visual Basic Language Timer problems.

paul_1966

HI!

My program shall program EEPROMs.

The reason why i use several subroutine calls is that i wanted to use one timer to drive my program.

Previously i used one timer for each subroutine, but i got some strange behavoiur then and suspected

that it may be related to the use of several timers and conmmunicating between then using variables.

So i decided to go for a solotion using one timer instead.

tmrReadSub() handles writing and reading of data to the equipment used for programming devices.

It communicates over RS232. It uses the functions write, read, open etc from the RS 232 class.

SecTimerSub() and timer1Sub() handle the internal state machines. Transition between states (program, verify, etc.).

They get information via variables set in tmrReadSub() when the current mission is completed such as program OK

or verify OK.

tmrStartupSequenceSub() is an initiallization sub called only at startup. It performs file access, uses a InputBox

to get some information from the user.

My problem is still that i will get several instances of the timer sub on the stack, even if i set it to disabled.

Then i will see different values of one variable for the two instances.

For example: I have a counter that count up by one every time i enter timer subroutine. If i have set a breakpoint inside the sub then i can see my counter working properly (1,2,3,4,5,6,7 ...)

Suddenly the counter becomes 0 and if i continue execution after that the displayed values of my counter is:

12, 0, 13 ,1 ,14 ,2 ...

It seems like i have two instances of my timer executing in parallell

//Paul






Re: Visual Basic Language Timer problems.

PEng1

I recomend that you look at either of two options, use the System.Timers.Timer which is more accurate and capable of moving across threads, or use a BackgroundWorker to run the code in the background. I use a Free running background thread to communicate with a network of PLC's which are very similar to EEPROMs, thanks to the expert help of one SJWhitley it works like a charm.




Re: Visual Basic Language Timer problems.

spotty

If your using serial ports - you do realize that these will run on a different thread to the UI.





Re: Visual Basic Language Timer problems.

paul_1966

Hi!

I have solved my timer problem. It was probably some kind of thread problem that i cannot understand.

I had my timer created in the form at design time. Then i could not set the Shared property.

Instead i define my timer variable myself as

Public Shared Timer1 As New Windows.Forms.Timer

this way i could set the Shared property.

Now it works fine!

//Paul