|
The code I posted was for VB.NET. You're using C#, which replaces the angle brackets with square ones.
|
|
|
|
|
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 = ""
Private mPassword As String = ""
Private mIP As String = ""
Private mStartSession As Long = 0
Private mLogged As Boolean = False
Private WithEvents mWUTimer As System.Windows.Forms.Timer
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 are OK, I call the following sub:
Private Sub SetWUser(ByVal Name As String, ByVal IP As String)
Dim i As Int32
For i = 0 To (CGlobali.Users.Count - 1)
If (Name = CType(CGlobali.Users.Item(i), CWebUser).UserName) Then
CType(CGlobali.Users.Item(i), CWebUser).IP = IP
CType(CGlobali.Users.Item(i), CWebUser).Logged = True
CType(CGlobali.Users.Item(i), CWebUser).StartSession = DateTime.Now.Ticks
CType(CGlobali.Users.Item(i), CWebUser).WUTimer.Enabled = True
Exit Sub
End If
Next
End Sub
This sub should start the timer WUTimer of the specified user, and indeed the property WUTimer.Enabled is set to True, but the sub WUTimer_Tick is never executed as if it is not connected to the Tick event of the timer.
Can some one help me?
Thanks in advance.
I think I made some dumb mistake but I can not find it.
|
|
|
|
|
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)
|
|
|
|
|
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[^]
|
|
|
|
|
Ok, I take a look at this timer.
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.
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
Try changing your class so that it looks like this:
Public Class CWebUser
Private mUserName As String = ""
Private mPassword As String = ""
Private mIP As String = ""
Private mStartSession As Long = 0
Private mLogged As Boolean = False
Private WithEvents mWUTimer As System.Windows.Forms.Timer
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.
|
|
|
|
|
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!
|
|
|
|
|
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
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).
|
|
|
|
|
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
If you can't read my code, try converting it here[^]
|
|
|
|
|
|
steve_9496613 wrote: I'm sure you're right but ..but I wasn't
Yes, that happens too.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
am working on a word document Programmatically using C#.net, Interop.Word, I have paragraphs in this document starting and ending with a "#".
Example:
Once upon a time, there was a #little girl who lived in a village near the forest. Whenever she went out, the little girl wore a red riding cloak, so everyone in the village called her Little Red Riding Hood.#
One morning, Little Red Riding Hood #asked her mother if she could go to visit her grandmother as it had been awhile since they'd seen each other.#
"That's a good idea," her mother said. #So they packed a nice basket for Little Red Riding Hood to take to her grandmother.#
Now between all # I need the text be bold
Any HELP!!!
|
|
|
|
|
nagham_4ng wrote: Any HELP!!!
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Thx Eddy, I know how to deal with word documents and ranges, my prob here is that I don't know the index of the characters #, plus that each 2 consecutive # make one range
|
|
|
|
|
nagham_4ng wrote: my prob here is that I don't know the index of the characters # You'd need to search for it.
nagham_4ng wrote: plus that each 2 consecutive # make one range When you find one, turn bold on. Find another, turn it off.
How to: Programmatically Search for and Replace Text in Documents[^]
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Hi,
I am trying to use RichTextBox to display a RTF file created by Microsoft Word.
I am using the
RichTextBox1.LoadFile(@"c:\test.rtf"); to load the data into the box.
Everything is fine except all the embedded graphics show up let justified.
The Word RTF file has them set to be behind the text and absolute positioned so there can be in middle of text field or where ever.
Is there a easy solution for this?
FWIW, it is a view for a help file that I wish to have icons and images included in.
Thanks in advance
Highflier
.net c# winforms
|
|
|
|
|
Word is more advanced than the RTB-component. I'd recommend using HTML.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|