Timer / UI thread issue

S

Smokey Grindle

I have my entire program in a Sub Main class and it runs as a notify tray
icon 99% of the time... inside the program there is a timer whihc is
declared like this

Dim WithEvents MyTimer As New System.Timers.Timer(20000)

the timer checks every 20 seconds a website to pull information from... if a
date is found on the page it looks at, it makes a list of dates and places
them into a date collection declared like this

Dim CurrentDates As New List(Of DateTime)

I also have a "dates form" that will pop up on screen if a date is found.
This is a form that never closes it is only shown and hidden from the
user... now every 20 seconds when the timer "ticks" my web task happens
(cant post code, because of information that is in it) this works perfecly
fine! but when we included the form in the mix, the problems started... the
web part works correctly, but the form is lagging... it will pop up, and
take forever to show the list of dates in its listbox.. then it shows "not
responding" in the titlebar after a second, then after a couple more seconds
it redraws correctly, then it lags again... at one point i was getting
"illegal cross thread.." something or other i forget exceptions when I
opened the form... I tried doing this with delegates but no real changes
that I noticed... here is how I am calling the forms now



when it has to hide

If Me.frmDates.InvokeRequired Then

Me.frmDates.Invoke(New HideDateForm(AddressOf ProcHideDateForm))

Else

Me.frmDates.Hide()

End If

when it has to show

If Me.frmDates.InvokeRequired Then

Me.frmDates.Invoke(New ShowDateForm(AddressOf ProcShowDateForm))

Else

Me.frmDates.Show()

End If

the delegate declerations



Public Delegate Sub ShowDateForm()

Public Delegate Sub HideDateForm()

Public Delegate Sub SendDatesToForm(ByVal currentDates As List(Of DateTime))

Public Sub ProcShowDateForm()

Me.frmDates.Show()

End Sub

Public Sub ProcHideDateForm()

Me.frmDates.Hide()

End Sub

Public Sub ProcSendDatesToForm(ByVal currentDates As List(Of DateTime))

Me.frmDates.LastDates = currentDates

End Sub





and the form code

==========================

Imports System.Text.RegularExpressions

Public Class frmDates

Private m_lastHTML As New List(Of DateTime)

''' <summary>

''' Last HTML response

''' </summary>

''' <value></value>

''' <returns></returns>

''' <remarks></remarks>

Public Property LastDates() As List(Of DateTime)

Get

Return m_lastHTML

End Get

Set(ByVal value As List(Of DateTime))

m_lastHTML = value

Me.lstDates.BeginUpdate()

Me.lstDates.Items.Clear()

For Each d As DateTime In LastDates

Me.lstDates.Items.Add(d.ToShortDateString)

Next

Me.lstDates.EndUpdate()

End Set

End Property




Public Sub New()

' This call is required by the Windows Form Designer.

InitializeComponent()

' Add any initialization after the InitializeComponent() call.

End Sub

Protected Overrides Sub OnClosing(ByVal e As
System.ComponentModel.CancelEventArgs)

e.Cancel = True

MyBase.OnClosing(e)

Me.Hide()

End Sub

End Class





if anyone could PLEASE help me with whats going on i'd really appriciate it,
thanks!
 
G

Guest

at one point i was getting
"illegal cross thread.." something or other i forget exceptions when I
opened the form... I tried doing this with delegates but no real changes
that I noticed... here is how I am calling the forms now

Forms are single threaded.

You need to check Invoke the call to the form.

Do a google search on Illegal Cross Thread error and you'll find plenty of
examples.
 
S

Smokey Grindel

this code is already based on that though! and when I tried to use delegates
and calling them correctly no change happened in speed
 
G

Guest

this code is already based on that though! and when I tried to use
delegates and calling them correctly no change happened in speed

How many dates are you posting to the form?
 
S

Smokey Grindel

at most 20... its nothing complex thats why I cant figure out the speed slow
down.... I had a Net.Mail request right after it thought thought it was
causing it took it out and made it Asyncronous and the speed got about 50%
better but still a lag... I havent seen any cross thread errors in a while
though, they seemed to randomly happen even though I am using delegates and
invoking them correctly....
 
A

Armin Zingler

Smokey Grindle said:
I have my entire program in a Sub Main class and it runs as a notify
tray icon 99% of the time... inside the program there is a timer
whihc is declared like this

Dim WithEvents MyTimer As New System.Timers.Timer(20000)

the timer checks every 20 seconds a website to pull information
from... if a date is found on the page it looks at, it makes a list
of dates and places them into a date collection declared like this

Dim CurrentDates As New List(Of DateTime)

[...]

I don't see any problem in your code. One thing that might happen is that
the Form references the same List(Of DateTime) object as the thread (the
Timer thread) that pushes the List into the Form's thread. Maybe this can
lead to none-thread safe operations on the List. I don't know whether the
List is used in the Timer's event handler after Invoke has returned and
simultaneously the Form thread does the same. Though, rather improbable.

Have you tried narrowing the problem down to where the time is spent? Have
you tried logging the time spent in the invoked procedures? How long does
filling the list take? How long does the Invoke call take? And so on. Apart
from the usual debugging that's what I would probably do, but it's hard to
say from here.


Armin
 
S

Smokey Grindel

I need to run it through a profiler still, so its on my list of things to
do... I think it happens when the list loads on the form from what I've seen
stepping through the code

Armin Zingler said:
Smokey Grindle said:
I have my entire program in a Sub Main class and it runs as a notify
tray icon 99% of the time... inside the program there is a timer
whihc is declared like this

Dim WithEvents MyTimer As New System.Timers.Timer(20000)

the timer checks every 20 seconds a website to pull information
from... if a date is found on the page it looks at, it makes a list
of dates and places them into a date collection declared like this

Dim CurrentDates As New List(Of DateTime)

[...]

I don't see any problem in your code. One thing that might happen is that
the Form references the same List(Of DateTime) object as the thread (the
Timer thread) that pushes the List into the Form's thread. Maybe this can
lead to none-thread safe operations on the List. I don't know whether the
List is used in the Timer's event handler after Invoke has returned and
simultaneously the Form thread does the same. Though, rather improbable.

Have you tried narrowing the problem down to where the time is spent? Have
you tried logging the time spent in the invoked procedures? How long does
filling the list take? How long does the Invoke call take? And so on.
Apart from the usual debugging that's what I would probably do, but it's
hard to say from here.


Armin
 
S

Smokey Grindel

well changed the System.Timers.Timer to System.Windows.Forms.Timer and the
problem disappeared... so definatly think its a cross thread problem....
humm not to figure out why... what is the major difference between the two?
I know Timers.Timer is in its own thread, but will that cause any problems?
the UI thread really does nothing in this program besides show a form with
dates on it sometimes... the only thing doing on was the tick event that
caused the web check... any differences I sould look out for?

Smokey Grindel said:
I need to run it through a profiler still, so its on my list of things to
do... I think it happens when the list loads on the form from what I've
seen stepping through the code

Armin Zingler said:
Smokey Grindle said:
I have my entire program in a Sub Main class and it runs as a notify
tray icon 99% of the time... inside the program there is a timer
whihc is declared like this

Dim WithEvents MyTimer As New System.Timers.Timer(20000)

the timer checks every 20 seconds a website to pull information
from... if a date is found on the page it looks at, it makes a list
of dates and places them into a date collection declared like this

Dim CurrentDates As New List(Of DateTime)

[...]

I don't see any problem in your code. One thing that might happen is that
the Form references the same List(Of DateTime) object as the thread (the
Timer thread) that pushes the List into the Form's thread. Maybe this can
lead to none-thread safe operations on the List. I don't know whether the
List is used in the Timer's event handler after Invoke has returned and
simultaneously the Form thread does the same. Though, rather improbable.

Have you tried narrowing the problem down to where the time is spent?
Have
you tried logging the time spent in the invoked procedures? How long does
filling the list take? How long does the Invoke call take? And so on.
Apart from the usual debugging that's what I would probably do, but it's
hard to say from here.


Armin
 

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