Timer / UI thread issue

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Smokey Grindle

    #1

    Timer / UI thread issue

    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.T imer(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.Inv okeRequired Then

    Me.frmDates.Inv oke(New HideDateForm(Ad dressOf ProcHideDateFor m))

    Else

    Me.frmDates.Hid e()

    End If

    when it has to show

    If Me.frmDates.Inv okeRequired Then

    Me.frmDates.Inv oke(New ShowDateForm(Ad dressOf ProcShowDateFor m))

    Else

    Me.frmDates.Sho w()

    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 ProcShowDateFor m()

    Me.frmDates.Sho w()

    End Sub

    Public Sub ProcHideDateFor m()

    Me.frmDates.Hid e()

    End Sub

    Public Sub ProcSendDatesTo Form(ByVal currentDates As List(Of DateTime))

    Me.frmDates.Las tDates = currentDates

    End Sub





    and the form code

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

    Imports System.Text.Reg ularExpressions

    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.Beg inUpdate()

    Me.lstDates.Ite ms.Clear()

    For Each d As DateTime In LastDates

    Me.lstDates.Ite ms.Add(d.ToShor tDateString)

    Next

    Me.lstDates.End Update()

    End Set

    End Property




    Public Sub New()

    ' This call is required by the Windows Form Designer.

    InitializeCompo nent()

    ' Add any initialization after the InitializeCompo nent() call.

    End Sub

    Protected Overrides Sub OnClosing(ByVal e As
    System.Componen tModel.CancelEv entArgs)

    e.Cancel = True

    MyBase.OnClosin g(e)

    Me.Hide()

    End Sub

    End Class





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


  • Spam Catcher

    #2
    Re: Timer / UI thread issue

    "Smokey Grindle" <nospam@dontspa mme.comwrote in news:OkNBxtvRHH A.4252
    @TK2MSFTNGP05.p hx.gbl:
    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.

    Comment

    • Smokey Grindel

      #3
      Re: Timer / UI thread issue

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

      "Spam Catcher" <spamhoneypot@r ogers.comwrote in message
      news:Xns98CBA3C 96DE1Eusenethon eypotrogers@127 .0.0.1...
      "Smokey Grindle" <nospam@dontspa mme.comwrote in news:OkNBxtvRHH A.4252
      @TK2MSFTNGP05.p hx.gbl:
      >
      > 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.

      Comment

      • Spam Catcher

        #4
        Re: Timer / UI thread issue

        "Smokey Grindel" <nospam@nospam. comwrote in
        news:eU28huyRHH A.388@TK2MSFTNG P04.phx.gbl:
        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?

        Comment

        • Smokey Grindel

          #5
          Re: Timer / UI thread issue

          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....

          "Spam Catcher" <spamhoneypot@r ogers.comwrote in message
          news:Xns98CBCB7 1630D2usenethon eypotrogers@127 .0.0.1...
          "Smokey Grindel" <nospam@nospam. comwrote in
          news:eU28huyRHH A.388@TK2MSFTNG P04.phx.gbl:
          >
          >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?

          Comment

          • Armin Zingler

            #6
            Re: Timer / UI thread issue

            "Smokey Grindle" <nospam@dontspa mme.comschrieb
            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.T imer(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

            Comment

            • Smokey Grindel

              #7
              Re: Timer / UI thread issue

              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" <az.nospam@free net.dewrote in message
              news:u2lSKA8RHH A.4028@TK2MSFTN GP03.phx.gbl...
              "Smokey Grindle" <nospam@dontspa mme.comschrieb
              >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.T imer(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

              Comment

              • Smokey Grindel

                #8
                Re: Timer / UI thread issue

                well changed the System.Timers.T imer 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" <nospam@nospam. comwrote in message
                news:u2Vi1G8RHH A.388@TK2MSFTNG P04.phx.gbl...
                >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" <az.nospam@free net.dewrote in message
                news:u2lSKA8RHH A.4028@TK2MSFTN GP03.phx.gbl...
                >"Smokey Grindle" <nospam@dontspa mme.comschrieb
                >>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.T imer(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
                >simultaneous ly 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
                >
                >

                Comment

                Working...