Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. [VB.NET 2008] A runtime created Timer doesn't work

[VB.NET 2008] A runtime created Timer doesn't work

Scheduled Pinned Locked Moved .NET (Core and Framework)
csharpsysadminsecurity
18 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S steve_9496613

    Hi everybody, my application runs on WinCE 5 device, it communicates with a PLC via modbus/TCP and it integrates a web server (code found in the article "Create your own Web Server using C#"). I'm trying to manage the remote access (from web browser) with user authentication and session timeout. Every user is an object of the following class:

    Public Class CWebUser

    Private mUserName As String = "" 'nome utente
    Private mPassword As String = "" 'MD5 della password
    Private mIP As String = "" 'IP di provenienza della connessione
    Private mStartSession As Long = 0 'istante di inizio della connessione come ticks
    Private mLogged As Boolean = False 'flag per sapere se l'utente è loggato
    Private WithEvents mWUTimer As System.Windows.Forms.Timer 'timer per gestire la durata della connessione

    Sub New()
    mWUTimer = New System.Windows.Forms.Timer()
    mWUTimer.Enabled = False
    mWUTimer.Interval = 10000
    AddHandler (mWUTimer.Tick), AddressOf WUTimer_Tick
    End Sub

    Private Sub WUTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
    If ((DateTime.Now.Ticks - mStartSession) > (CGlobali.WebSessionDuration * 10000000)) Then
    mWUTimer.Enabled = False
    mIP = ""
    mLogged = False
    End If
    End Sub

    Public Property UserName() As String
    Get
    Return mUserName
    End Get
    Set(ByVal value As String)
    mUserName = value
    End Set
    End Property

    Public Property Password() As String
    Get
    Return mPassword
    End Get
    Set(ByVal value As String)
    mPassword = value
    End Set
    End Property

    Public Property IP() As String
    Get
    Return mIP
    End Get
    Set(ByVal value As String)
    mIP = value
    End Set
    End Property

    Public Property StartSession() As Long
    Get
    Return mStartSession
    End Get
    Set(ByVal value As Long)
    mStartSession = value
    End Set
    End Property

    Public Property Logged() As Boolean
    Get
    Return mLogged
    End Get
    Set(ByVal value As Boolean)
    mLogged = value
    End Set
    End Property

    Public Property WUTimer() As System.Windows.Forms.Timer
    Get
    Return mWUTimer
    End Get
    Set(ByVal value As System.Windows.Forms.Timer)
    mWUTimer = value
    End Set
    End Property

    End Class

    When the application starts, it creates a list of authorized users. When someone logs in, if UserName and Password

    P Offline
    P Offline
    Pete OHanlon
    wrote on last edited by
    #2

    I'm pretty sure you have to Start the timer. If I were you, I would refactor the class so that you had a method like this in it:

    Public Sub Start(ByVal IPAddress As String, ByVal IsLogged As Boolean)
    IP = IPAddress
    Logged = IsLogged
    StartSession = DateTime.Now.Ticks
    mUTimer.Enabled = True
    muTimer.Start
    End Sub

    Then you replace a lot of your code with a simple

    CType(CGlobali.Users.Item(i), CWebUser).Start(IP, True)

    Chill _Maxxx_
    CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

    S 1 Reply Last reply
    0
    • P Pete OHanlon

      I'm pretty sure you have to Start the timer. If I were you, I would refactor the class so that you had a method like this in it:

      Public Sub Start(ByVal IPAddress As String, ByVal IsLogged As Boolean)
      IP = IPAddress
      Logged = IsLogged
      StartSession = DateTime.Now.Ticks
      mUTimer.Enabled = True
      muTimer.Start
      End Sub

      Then you replace a lot of your code with a simple

      CType(CGlobali.Users.Item(i), CWebUser).Start(IP, True)

      Chill _Maxxx_
      CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

      S Offline
      S Offline
      steve_9496613
      wrote on last edited by
      #3

      Thanks Pete, but unfortunately my application runs on Windows CE, so it uses the Compact Framework, not the full .NET and in Compact Framework there is not the method Start for the timer.

      P 1 Reply Last reply
      0
      • S steve_9496613

        Thanks Pete, but unfortunately my application runs on Windows CE, so it uses the Compact Framework, not the full .NET and in Compact Framework there is not the method Start for the timer.

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #4

        Ah. In that case, I would probably look at using System.Threading.Timer instead. Documentation[^]

        Chill _Maxxx_
        CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

        S 2 Replies Last reply
        0
        • P Pete OHanlon

          Ah. In that case, I would probably look at using System.Threading.Timer instead. Documentation[^]

          Chill _Maxxx_
          CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

          S Offline
          S Offline
          steve_9496613
          wrote on last edited by
          #5

          Ok, I take a look at this timer. Thanks

          1 Reply Last reply
          0
          • P Pete OHanlon

            Ah. In that case, I would probably look at using System.Threading.Timer instead. Documentation[^]

            Chill _Maxxx_
            CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

            S Offline
            S Offline
            steve_9496613
            wrote on last edited by
            #6

            Sorry but I don't know how to use this kind of timer in the way I need (property of an object). Any other suggestions about how to make a System.Windows.Forms.Timer work as I need? Thanks

            P 1 Reply Last reply
            0
            • S steve_9496613

              Sorry but I don't know how to use this kind of timer in the way I need (property of an object). Any other suggestions about how to make a System.Windows.Forms.Timer work as I need? Thanks

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #7

              Put a breakpoint in your timer tick event and see if it's being hit when you run it in debug. Does it hit the method or not? My suspicion is that it is hitting the method, but there's a bug in the if statement.

              Chill _Maxxx_
              CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

              S 1 Reply Last reply
              0
              • P Pete OHanlon

                Put a breakpoint in your timer tick event and see if it's being hit when you run it in debug. Does it hit the method or not? My suspicion is that it is hitting the method, but there's a bug in the if statement.

                Chill _Maxxx_
                CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                S Offline
                S Offline
                steve_9496613
                wrote on last edited by
                #8

                I put a breakpoint on line

                If ((DateTime.Now.Ticks - mStartSession) > (CGlobali.WebSessionDuration * 10000000)) Then

                but the debug never stop there

                P 1 Reply Last reply
                0
                • S steve_9496613

                  I put a breakpoint on line

                  If ((DateTime.Now.Ticks - mStartSession) > (CGlobali.WebSessionDuration * 10000000)) Then

                  but the debug never stop there

                  P Offline
                  P Offline
                  Pete OHanlon
                  wrote on last edited by
                  #9

                  And if you set the breakpoint in SetWUser? Is it actually executing this line?

                  CType(CGlobali.Users.Item(i), CWebUser).WUTimer.Enabled = True

                  Chill _Maxxx_
                  CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                  S 1 Reply Last reply
                  0
                  • S steve_9496613

                    Hi everybody, my application runs on WinCE 5 device, it communicates with a PLC via modbus/TCP and it integrates a web server (code found in the article "Create your own Web Server using C#"). I'm trying to manage the remote access (from web browser) with user authentication and session timeout. Every user is an object of the following class:

                    Public Class CWebUser

                    Private mUserName As String = "" 'nome utente
                    Private mPassword As String = "" 'MD5 della password
                    Private mIP As String = "" 'IP di provenienza della connessione
                    Private mStartSession As Long = 0 'istante di inizio della connessione come ticks
                    Private mLogged As Boolean = False 'flag per sapere se l'utente è loggato
                    Private WithEvents mWUTimer As System.Windows.Forms.Timer 'timer per gestire la durata della connessione

                    Sub New()
                    mWUTimer = New System.Windows.Forms.Timer()
                    mWUTimer.Enabled = False
                    mWUTimer.Interval = 10000
                    AddHandler (mWUTimer.Tick), AddressOf WUTimer_Tick
                    End Sub

                    Private Sub WUTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
                    If ((DateTime.Now.Ticks - mStartSession) > (CGlobali.WebSessionDuration * 10000000)) Then
                    mWUTimer.Enabled = False
                    mIP = ""
                    mLogged = False
                    End If
                    End Sub

                    Public Property UserName() As String
                    Get
                    Return mUserName
                    End Get
                    Set(ByVal value As String)
                    mUserName = value
                    End Set
                    End Property

                    Public Property Password() As String
                    Get
                    Return mPassword
                    End Get
                    Set(ByVal value As String)
                    mPassword = value
                    End Set
                    End Property

                    Public Property IP() As String
                    Get
                    Return mIP
                    End Get
                    Set(ByVal value As String)
                    mIP = value
                    End Set
                    End Property

                    Public Property StartSession() As Long
                    Get
                    Return mStartSession
                    End Get
                    Set(ByVal value As Long)
                    mStartSession = value
                    End Set
                    End Property

                    Public Property Logged() As Boolean
                    Get
                    Return mLogged
                    End Get
                    Set(ByVal value As Boolean)
                    mLogged = value
                    End Set
                    End Property

                    Public Property WUTimer() As System.Windows.Forms.Timer
                    Get
                    Return mWUTimer
                    End Get
                    Set(ByVal value As System.Windows.Forms.Timer)
                    mWUTimer = value
                    End Set
                    End Property

                    End Class

                    When the application starts, it creates a list of authorized users. When someone logs in, if UserName and Password

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #10

                    steve_9496613 wrote:

                    mWUTimer = New System.Windows.Forms.Timer()

                    According to the documentation[^], that class isn't supported. The Threading-timer[^] seems to be supported.

                    Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                    S 1 Reply Last reply
                    0
                    • P Pete OHanlon

                      And if you set the breakpoint in SetWUser? Is it actually executing this line?

                      CType(CGlobali.Users.Item(i), CWebUser).WUTimer.Enabled = True

                      Chill _Maxxx_
                      CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                      S Offline
                      S Offline
                      steve_9496613
                      wrote on last edited by
                      #11

                      yes, the line is executed and the property WUTimer.Enabled is really set to true, to check it I wrote just after that line something like this:

                      Dim aaa As Boolean
                      aaa = CType(CGlobali.Users.Item(i), CWebUser).WUTimer.Enabled

                      and aaa become true. It seems like the instruction

                      AddHandler (mWUTimer.Tick), AddressOf WUTimer_Tick

                      does not link the sub WUTimer_Tick to the event mWUTimer.Tick

                      P 1 Reply Last reply
                      0
                      • L Lost User

                        steve_9496613 wrote:

                        mWUTimer = New System.Windows.Forms.Timer()

                        According to the documentation[^], that class isn't supported. The Threading-timer[^] seems to be supported.

                        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                        S Offline
                        S Offline
                        steve_9496613
                        wrote on last edited by
                        #12

                        Hi Eddy, thanks for the replay. Your link to the documentation about the System.Windows.Forms.Timer refers to .Net Framework 4.5 but, for this time, I'm luky and in Compact Framework 3.5 (and also .NET Framework 3.5) this timer is supported. The System.Threading.Timer is supported, as Pete O'Hanlon suggested, but I have some problem to put one of this timer in my class as a property (I mean like I did with the Form.Timer).

                        L 1 Reply Last reply
                        0
                        • S steve_9496613

                          Hi Eddy, thanks for the replay. Your link to the documentation about the System.Windows.Forms.Timer refers to .Net Framework 4.5 but, for this time, I'm luky and in Compact Framework 3.5 (and also .NET Framework 3.5) this timer is supported. The System.Threading.Timer is supported, as Pete O'Hanlon suggested, but I have some problem to put one of this timer in my class as a property (I mean like I did with the Form.Timer).

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #13

                          steve_9496613 wrote:

                          Your link to the documentation about the System.Windows.Forms.Timer refers to .Net Framework 4.5

                          Only for the member-listing; you can select the version with a dropdown at the top. The "version information" and "platform" heading isn't limited to .NET 4.5 - it simply lists where this class is supported.

                          steve_9496613 wrote:

                          Compact Framework 3.5 (and also .NET Framework 3.5) this timer is supported.

                          Not according to MSDN.

                          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                          S 1 Reply Last reply
                          0
                          • L Lost User

                            steve_9496613 wrote:

                            Your link to the documentation about the System.Windows.Forms.Timer refers to .Net Framework 4.5

                            Only for the member-listing; you can select the version with a dropdown at the top. The "version information" and "platform" heading isn't limited to .NET 4.5 - it simply lists where this class is supported.

                            steve_9496613 wrote:

                            Compact Framework 3.5 (and also .NET Framework 3.5) this timer is supported.

                            Not according to MSDN.

                            Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                            S Offline
                            S Offline
                            steve_9496613
                            wrote on last edited by
                            #14

                            I'm sure you're right but the page http://msdn.microsoft.com/en-us/library/system.windows.forms.timer%28v=vs.90%29.aspx[^] is different from the page http://msdn.microsoft.com/en-us/library/system.windows.forms.timer%28v=vs.110%29.aspx[^] and the Version Information at the bottom of the pages are different. In my application I use some System.Windows.Forms.Timer and they work fine. I will try not to make it known to MSDN...

                            L 1 Reply Last reply
                            0
                            • S steve_9496613

                              I'm sure you're right but the page http://msdn.microsoft.com/en-us/library/system.windows.forms.timer%28v=vs.90%29.aspx[^] is different from the page http://msdn.microsoft.com/en-us/library/system.windows.forms.timer%28v=vs.110%29.aspx[^] and the Version Information at the bottom of the pages are different. In my application I use some System.Windows.Forms.Timer and they work fine. I will try not to make it known to MSDN...

                              L Offline
                              L Offline
                              Lost User
                              wrote on last edited by
                              #15

                              steve_9496613 wrote:

                              I'm sure you're right but

                              ..but I wasn't :-D Yes, that happens too.

                              Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                              1 Reply Last reply
                              0
                              • S steve_9496613

                                yes, the line is executed and the property WUTimer.Enabled is really set to true, to check it I wrote just after that line something like this:

                                Dim aaa As Boolean
                                aaa = CType(CGlobali.Users.Item(i), CWebUser).WUTimer.Enabled

                                and aaa become true. It seems like the instruction

                                AddHandler (mWUTimer.Tick), AddressOf WUTimer_Tick

                                does not link the sub WUTimer_Tick to the event mWUTimer.Tick

                                P Offline
                                P Offline
                                Pete OHanlon
                                wrote on last edited by
                                #16

                                Try changing your class so that it looks like this:

                                Public Class CWebUser

                                Private mUserName As String = "" 'nome utente
                                Private mPassword As String = "" 'MD5 della password
                                Private mIP As String = "" 'IP di provenienza della connessione
                                Private mStartSession As Long = 0 'istante di inizio della connessione come ticks
                                Private mLogged As Boolean = False 'flag per sapere se l'utente è loggato
                                Private WithEvents mWUTimer As System.Windows.Forms.Timer 'timer per gestire la durata della connessione

                                Sub New()
                                End Sub

                                Private Sub WUTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
                                If ((DateTime.Now.Ticks - mStartSession) > (CGlobali.WebSessionDuration * 10000000)) Then
                                mWUTimer.Enabled = False
                                mIP = ""
                                mLogged = False
                                End If
                                End Sub

                                Public Property UserName() As String
                                Get
                                Return mUserName
                                End Get
                                Set(ByVal value As String)
                                mUserName = value
                                End Set
                                End Property

                                Public Property Password() As String
                                Get
                                Return mPassword
                                End Get
                                Set(ByVal value As String)
                                mPassword = value
                                End Set
                                End Property

                                Public Property IP() As String
                                Get
                                Return mIP
                                End Get
                                Set(ByVal value As String)
                                mIP = value
                                End Set
                                End Property

                                Public Property StartSession() As Long
                                Get
                                Return mStartSession
                                End Get
                                Set(ByVal value As Long)
                                mStartSession = value
                                End Set
                                End Property

                                Public Property Logged() As Boolean
                                Get
                                Return mLogged
                                End Get
                                Set(ByVal value As Boolean)
                                mLogged = value
                                End Set
                                End Property

                                Public Property WUTimer() As System.Windows.Forms.Timer
                                Get
                                Return mWUTimer
                                End Get
                                Set(ByVal value As System.Windows.Forms.Timer)
                                mWUTimer = value
                                End Set
                                End Property

                                Public Sub EnableTimer(Byval IpAddress As String, ByVal Name As String)
                                IP = IpAddress
                                UserName = Name
                                mWUTimer = New System.Windows.Forms.Timer()
                                mWUTimer.Interval = 10000
                                AddHandler (mWUTimer.Tick), AddressOf WUTimer_Tick
                                mWUTimer.Enabled = True
                                End Sub
                                End Class

                                Then call EnableTimer from your loop.

                                Chill _Maxxx_
                                CodeStash - Online Snippet Management | My blog |

                                S 1 Reply Last reply
                                0
                                • P Pete OHanlon

                                  Try changing your class so that it looks like this:

                                  Public Class CWebUser

                                  Private mUserName As String = "" 'nome utente
                                  Private mPassword As String = "" 'MD5 della password
                                  Private mIP As String = "" 'IP di provenienza della connessione
                                  Private mStartSession As Long = 0 'istante di inizio della connessione come ticks
                                  Private mLogged As Boolean = False 'flag per sapere se l'utente è loggato
                                  Private WithEvents mWUTimer As System.Windows.Forms.Timer 'timer per gestire la durata della connessione

                                  Sub New()
                                  End Sub

                                  Private Sub WUTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
                                  If ((DateTime.Now.Ticks - mStartSession) > (CGlobali.WebSessionDuration * 10000000)) Then
                                  mWUTimer.Enabled = False
                                  mIP = ""
                                  mLogged = False
                                  End If
                                  End Sub

                                  Public Property UserName() As String
                                  Get
                                  Return mUserName
                                  End Get
                                  Set(ByVal value As String)
                                  mUserName = value
                                  End Set
                                  End Property

                                  Public Property Password() As String
                                  Get
                                  Return mPassword
                                  End Get
                                  Set(ByVal value As String)
                                  mPassword = value
                                  End Set
                                  End Property

                                  Public Property IP() As String
                                  Get
                                  Return mIP
                                  End Get
                                  Set(ByVal value As String)
                                  mIP = value
                                  End Set
                                  End Property

                                  Public Property StartSession() As Long
                                  Get
                                  Return mStartSession
                                  End Get
                                  Set(ByVal value As Long)
                                  mStartSession = value
                                  End Set
                                  End Property

                                  Public Property Logged() As Boolean
                                  Get
                                  Return mLogged
                                  End Get
                                  Set(ByVal value As Boolean)
                                  mLogged = value
                                  End Set
                                  End Property

                                  Public Property WUTimer() As System.Windows.Forms.Timer
                                  Get
                                  Return mWUTimer
                                  End Get
                                  Set(ByVal value As System.Windows.Forms.Timer)
                                  mWUTimer = value
                                  End Set
                                  End Property

                                  Public Sub EnableTimer(Byval IpAddress As String, ByVal Name As String)
                                  IP = IpAddress
                                  UserName = Name
                                  mWUTimer = New System.Windows.Forms.Timer()
                                  mWUTimer.Interval = 10000
                                  AddHandler (mWUTimer.Tick), AddressOf WUTimer_Tick
                                  mWUTimer.Enabled = True
                                  End Sub
                                  End Class

                                  Then call EnableTimer from your loop.

                                  Chill _Maxxx_
                                  CodeStash - Online Snippet Management | My blog |

                                  S Offline
                                  S Offline
                                  steve_9496613
                                  wrote on last edited by
                                  #17

                                  Thanks for your effort. I did what you suggest but the result is the same: the tick event is not fired. I fear it can be a problem related to thread. The web server is a class with a thread that start the listener to receive connections from remote. In the main thread of the application I instantiate an object WebServer and in this way I start its thread (the thread is created and started in the sub New() of the class). The list of users (objects of the class CWebUser) is created in the main thread of the application. The sub SetWUser(...) that enable the WUTimer is part of the class of the web server and is called by the function VerifyLogInData(...) called in the loop of the thread of the web server... Is it possible that the problem is that I try to use from thread X a method of an object created in thread Y?

                                  S 1 Reply Last reply
                                  0
                                  • S steve_9496613

                                    Thanks for your effort. I did what you suggest but the result is the same: the tick event is not fired. I fear it can be a problem related to thread. The web server is a class with a thread that start the listener to receive connections from remote. In the main thread of the application I instantiate an object WebServer and in this way I start its thread (the thread is created and started in the sub New() of the class). The list of users (objects of the class CWebUser) is created in the main thread of the application. The sub SetWUser(...) that enable the WUTimer is part of the class of the web server and is called by the function VerifyLogInData(...) called in the loop of the thread of the web server... Is it possible that the problem is that I try to use from thread X a method of an object created in thread Y?

                                    S Offline
                                    S Offline
                                    steve_9496613
                                    wrote on last edited by
                                    #18

                                    Yes it is... I have solved the problem enabling the timer from a sub in the main thread and colling this sub with the Invoke function from the web server thread. Bye!

                                    1 Reply Last reply
                                    0
                                    Reply
                                    • Reply as topic
                                    Log in to reply
                                    • Oldest to Newest
                                    • Newest to Oldest
                                    • Most Votes


                                    • Login

                                    • Don't have an account? Register

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • World
                                    • Users
                                    • Groups