[VB.NET 2008] A runtime created Timer doesn't work
-
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 connessioneSub New()
mWUTimer = New System.Windows.Forms.Timer()
mWUTimer.Enabled = False
mWUTimer.Interval = 10000
AddHandler (mWUTimer.Tick), AddressOf WUTimer_Tick
End SubPrivate 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 SubPublic Property UserName() As String
Get
Return mUserName
End Get
Set(ByVal value As String)
mUserName = value
End Set
End PropertyPublic Property Password() As String
Get
Return mPassword
End Get
Set(ByVal value As String)
mPassword = value
End Set
End PropertyPublic Property IP() As String
Get
Return mIP
End Get
Set(ByVal value As String)
mIP = value
End Set
End PropertyPublic Property StartSession() As Long
Get
Return mStartSession
End Get
Set(ByVal value As Long)
mStartSession = value
End Set
End PropertyPublic Property Logged() As Boolean
Get
Return mLogged
End Get
Set(ByVal value As Boolean)
mLogged = value
End Set
End PropertyPublic 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 PropertyEnd Class
When the application starts, it creates a list of authorized users. When someone logs in, if UserName and Password
-
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 connessioneSub New()
mWUTimer = New System.Windows.Forms.Timer()
mWUTimer.Enabled = False
mWUTimer.Interval = 10000
AddHandler (mWUTimer.Tick), AddressOf WUTimer_Tick
End SubPrivate 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 SubPublic Property UserName() As String
Get
Return mUserName
End Get
Set(ByVal value As String)
mUserName = value
End Set
End PropertyPublic Property Password() As String
Get
Return mPassword
End Get
Set(ByVal value As String)
mPassword = value
End Set
End PropertyPublic Property IP() As String
Get
Return mIP
End Get
Set(ByVal value As String)
mIP = value
End Set
End PropertyPublic Property StartSession() As Long
Get
Return mStartSession
End Get
Set(ByVal value As Long)
mStartSession = value
End Set
End PropertyPublic Property Logged() As Boolean
Get
Return mLogged
End Get
Set(ByVal value As Boolean)
mLogged = value
End Set
End PropertyPublic 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 PropertyEnd Class
When the application starts, it creates a list of authorized users. When someone logs in, if UserName and Password
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 SubThen 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 -
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 SubThen 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 easierThanks 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.
-
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.
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 -
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 easierOk, I take a look at this timer. Thanks
-
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 easierSorry 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
-
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
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 -
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 easierI put a breakpoint on line
If ((DateTime.Now.Ticks - mStartSession) > (CGlobali.WebSessionDuration * 10000000)) Then
but the debug never stop there
-
I put a breakpoint on line
If ((DateTime.Now.Ticks - mStartSession) > (CGlobali.WebSessionDuration * 10000000)) Then
but the debug never stop there
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 -
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 connessioneSub New()
mWUTimer = New System.Windows.Forms.Timer()
mWUTimer.Enabled = False
mWUTimer.Interval = 10000
AddHandler (mWUTimer.Tick), AddressOf WUTimer_Tick
End SubPrivate 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 SubPublic Property UserName() As String
Get
Return mUserName
End Get
Set(ByVal value As String)
mUserName = value
End Set
End PropertyPublic Property Password() As String
Get
Return mPassword
End Get
Set(ByVal value As String)
mPassword = value
End Set
End PropertyPublic Property IP() As String
Get
Return mIP
End Get
Set(ByVal value As String)
mIP = value
End Set
End PropertyPublic Property StartSession() As Long
Get
Return mStartSession
End Get
Set(ByVal value As Long)
mStartSession = value
End Set
End PropertyPublic Property Logged() As Boolean
Get
Return mLogged
End Get
Set(ByVal value As Boolean)
mLogged = value
End Set
End PropertyPublic 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 PropertyEnd Class
When the application starts, it creates a list of authorized users. When someone logs in, if UserName and Password
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[^]
-
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 easieryes, 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.Enabledand 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
-
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[^]
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).
-
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).
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[^]
-
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[^]
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...
-
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...
-
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.Enabledand 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
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 connessioneSub New()
End SubPrivate 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 SubPublic Property UserName() As String
Get
Return mUserName
End Get
Set(ByVal value As String)
mUserName = value
End Set
End PropertyPublic Property Password() As String
Get
Return mPassword
End Get
Set(ByVal value As String)
mPassword = value
End Set
End PropertyPublic Property IP() As String
Get
Return mIP
End Get
Set(ByVal value As String)
mIP = value
End Set
End PropertyPublic Property StartSession() As Long
Get
Return mStartSession
End Get
Set(ByVal value As Long)
mStartSession = value
End Set
End PropertyPublic Property Logged() As Boolean
Get
Return mLogged
End Get
Set(ByVal value As Boolean)
mLogged = value
End Set
End PropertyPublic 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 PropertyPublic 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 ClassThen call
EnableTimer
from your loop.Chill _Maxxx_
CodeStash - Online Snippet Management | My blog | -
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 connessioneSub New()
End SubPrivate 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 SubPublic Property UserName() As String
Get
Return mUserName
End Get
Set(ByVal value As String)
mUserName = value
End Set
End PropertyPublic Property Password() As String
Get
Return mPassword
End Get
Set(ByVal value As String)
mPassword = value
End Set
End PropertyPublic Property IP() As String
Get
Return mIP
End Get
Set(ByVal value As String)
mIP = value
End Set
End PropertyPublic Property StartSession() As Long
Get
Return mStartSession
End Get
Set(ByVal value As Long)
mStartSession = value
End Set
End PropertyPublic Property Logged() As Boolean
Get
Return mLogged
End Get
Set(ByVal value As Boolean)
mLogged = value
End Set
End PropertyPublic 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 PropertyPublic 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 ClassThen call
EnableTimer
from your loop.Chill _Maxxx_
CodeStash - Online Snippet Management | My blog |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?
-
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?
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!