Why doesn't timer tick

B

Brett

I have a timer enabled on a form with an interval of 200. I place a break
point on the first line in the timer tick event, which is a try. After a
few events go on, the timer seems to stop. My break point is no longer hit.
In the debugger, the timer shows enabled and the interval is still set.

Any ideas what may be happening or something else I can check?

Thanks,
Brett
 
C

Cor Ligthert

Brett,

There are at least 3 timers in Net. Therefore tell us which.

Do you as well disable the timer when it is fired in the event. Otherwise it
keeps on firing and than you will become crazy when setting a breakpoin and
debug. The events are throwed, however you see everytime another one.

I hope this help

Cor
 
B

Brett

I'm using System.Windows.Forms.Timer. Yes - I disable it in the timer event
but reenable it. If I set a break point outside of the timer event and
check the timer1.enabled property, which is true, shouldn't the timer be
firing?

Yes - the debugger does jump all over the place when the event is firing.
After a few actions in the software, the event stops firing but
timer1.enabled = true. I could post a bunch of code but was wondering why
timer1.enabled = true and the event in fact isn't firing. How can
timer1.enabled = true and the event not fire?

Thanks,
Brett
 
C

Cor Ligthert

Brett,

Can you show the code in that event?
(First copied in a notebook, than pasted back in a message otherwise it is
almost unreadable)


Cor
 
B

Brett

Cor Ligthert said:
Brett,

Can you show the code in that event?
(First copied in a notebook, than pasted back in a message otherwise it is
almost unreadable)


Cor

Here is the timer tick event code. This is the only place timer is being
disabled/enabled. The interval is set at 200. Thanks.



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

Dim CurThreadStart As ThreadStart
Dim CurThread As Thread
Dim ThreadCount As Integer
Dim MaxThreads As Integer = 5
Dim i As Integer

Try

If Not tcpListener.Pending() Then
Exit Sub
End If

Timer1.Enabled = False

If ActiveThreads > CInt(MaxThreads) Then
Timer1.Enabled = True
Exit Sub
End If

CurThreadStart = New ThreadStart(AddressOf Listen)

CurThread = New Thread(CurThreadStart)

CurThread.IsBackground = True
CurThread.Start()
SyncLock CurThread
ActiveThreads += 1
'write to log file
utility.WriteToFile(CurrentDirectory.GetCurrentDirectory,
LogFileExec, utility.CurrentDateTime(0) & " - ActiveThreads = " &
ActiveThreads)
End SyncLock

Timer1.Enabled = True

Catch ex As Exception
If InStr(ex.Message, "Not listening") Then
'write to log file
utility.WriteToFile(CurrentDirectory.GetCurrentDirectory,
LogFileError, utility.CurrentDateTime(0) & " - ERROR [Timer1_Tick] " &
ex.Message & vbCrLf)
Else

End If

End Try

End Sub
 
C

Cor Ligthert

Brett,

Before I start looking at that timer, I have the idea that what you want to
do is something I have looked in past a long time for and is easy to find
now in this newsgroup.

There are than two solutions.
Use a loop with in it as long there are threads active
\\\
do your testing
threading.thread.sleep(200)
applications.doevents
///
And what I use now, is let the thread throw an event when it is ready (and
give the information back using a parameter in that event) what I catch in
the mainthread.

However when you are doing something else, reply than.

Cor
 
B

Brett

Cor Ligthert said:
Brett,

Before I start looking at that timer, I have the idea that what you want
to do is something I have looked in past a long time for and is easy to
find now in this newsgroup.

There are than two solutions.
Use a loop with in it as long there are threads active
\\\
do your testing
threading.thread.sleep(200)
applications.doevents
///
And what I use now, is let the thread throw an event when it is ready (and
give the information back using a parameter in that event) what I catch
in the mainthread.

However when you are doing something else, reply than.

Cor

Could you give a small code example of what you mean on the above?

Thanks,
Brett
 
B

Brett

Cor Ligthert said:
Brett,

Before I start looking at that timer, I have the idea that what you want
to do is something I have looked in past a long time for and is easy to
find now in this newsgroup.

There are than two solutions.
Use a loop with in it as long there are threads active
\\\
do your testing
threading.thread.sleep(200)
applications.doevents
///
And what I use now, is let the thread throw an event when it is ready (and
give the information back using a parameter in that event) what I catch
in the mainthread.

However when you are doing something else, reply than.

Cor

Maybe one problem is everything being on the same thread. I'm doing this:

sub first
While true
--do something
thread.sleep(500)
If something else then
timer.enabled = false
exit while
end if
end while

timer.enable = true
end sub

sub timer_tick
-- check tcplistener.pending()
end sub

It's almost as if the timer doesn't really get a slice of time on the
current thread. I believe the timer_tick event should be place in another
class since it checks for pending() tcp/ip connections. If there is a
pending(), it will spawn a new thread for the while loop. Comments?

Thanks,
Brett
 
C

Cor Ligthert

Brett,

I mean don't use the forms timer for this, when the thread is sleeping it is
not giving a response.

You can use the threading.thread.timers which runs on seperated threads..

However that event to sent back or that loop with a thread sleep gave for me
the same effect.

However I have not that "good" expirience with those threading thread
timers. (I could not abort them even when the program was stopped).

Cor
 
B

Brett

The problem with threading.thread.timers is it runs from the ThreadPool and
will fire independant of the GUI. You then must use Invoke to update the
GUI controls to be sure the threads are in sync.

However, I will put the GUI on one thread and everything else into its own
class with another thread.

Brett
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top