asp.net timer

a8le

Well-known member
Joined
Oct 27, 2005
Messages
75
Programming Experience
5-10
hi all,

Can someone please help me find an article or tutorial on how to build or implement a scheculed timer in asp.net/vb.net? PLEASE!

I have search and search the net for such a tutorial and have found nothing. All I have been able to find are asp.net/c#.net tutorials. I am too much of a newbie to try and convert any of them.

I have search/Googled: asp.net timer, asp.net vb.net timer, vb.net timer scheduled timer asp.net, etc...

What I am trying to do is get a function from the behind code from my webpage (aspx) to write a log file on a daily basis.

I have tried using the built in Timer in VS.net Components>Timer but it didn't work.

Can someone please lead me in the right direction? Oh, I have noticed that everything is in milliseconds... will timers do 24hours? I ask this because, of the many articles I have read they have all demonstrate, short intervals, say 1,2,or3minutes. I believe 24 hours is like 84,600,000 milliseconds.

-a8le:confused:
 
The Timer's Interval property is an Integer, which has a maximum value in milliseconds equivalent to about 28 days. The System.Timers.Timer is the component to use. Set the Interval; handle the Elapsed event; set the AutoReset property to True if you want the event to be raised more than once.
 
Timer not working.

jmcilhinney,

What am I doing wrong here?....

PublicSub Timer()
Dim aTimer AsNew System.Timers.Timer
AddHandler aTimer.Elapsed, AddressOf OnTimedEvent
' Set the Interval to 5 seconds.
aTimer.Interval = 5000
aTimer.Enabled =
True
aTimer.AutoReset = True
EndSub
' Specify what you want to happen when the Elapsed event is raised.
PublicSub OnTimedEvent(ByVal sender AsObject, ByVal e As System.Timers.ElapsedEventArgs)
Log() ' This is the script that write the log, i know it works

EndSub

Also, where is the best place to call the timer function to start it? I doesn't seem right to have it in page load. It should be a one time thing. And I don't want to press a button to start it, actually I don't want to interact with it at all, thats why I have it to .AutoReset.

-a8le
 
I'm afraid I don't know too much about ASP.NET. I've only really developed for WinForms and things are obviously a bit different there. Hopefully someone with experience with using a Timer in ASP.NET will be able to answer you.
 
I found this and i am afraid that it is correct :(

I've never tried using a timer in asp.net so this may be wrong, but I doubt you will ever get that to work. Once you finish the postback the client has no connection to the server and the server has no connection to the client. That is how the web works. Your timer can't touch a page that is "gone". If you want your page to refresh at a certain interval, you need to write something in javascript to do a postback to the server and refetch the data.

Btw, if i was there i would go for windows service.

Regards ;)
 
kulrom ,

Hi, thank you for the knowledge. Can you please explain more about the windows service? Would this be possible if my site is hosted? What about javacript, isn't it client dependent as well?

There has got to be a way. Its 2005. :)

-a8le
 
Hi all,

I have decided not to do the timer thingy. Instead I am thinking about using a label that display "the amount of days since the process has ran."

I know that I have to "record/log" the last day it ran and then "record/log" the present day. Once that is done, compare the two dates and display "the amount of days since the process has ran."

Now my question is how do I do this with when date stamp come in this format: 12/7/05 ? It just isn't as simple as 12-5=7. :)

Thanks in advance.
-a8le
 
To create a Date object from a string you can use the Date.ParseExact method. If you know for a fact that you'll be getting the date string in a particular format you can specify that format and create a Date like this:
VB.NET:
Dim myDate As Date = Date.ParseExact(myString, "dd/MM/yy", Nothing)
 
You guys are complicating simple stuff! The man has two dates and need a number of days. Just use:

VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] FirstDate [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Date[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#0000ff]CDate[/COLOR][/SIZE][SIZE=2]("12/7/05").ToShortDateString
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] LastDate [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Date[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#0000ff]CDate[/COLOR][/SIZE][SIZE=2]("10/9/05").ToShortDateString
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] DaysCount [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = DateDiff(DateInterval.Day, FirstDate, LastDate).ToString
Response.Write(DaysCount)
[/SIZE]
 
sorry, i didn't realize that you guys replied. after getting your ideas i went looking deeper into asp.net date features... and found date.now.dayofyear

i simply compared the two integers. thanks for the help.
 
Back
Top