Anybody knows how to add a Countdown Timer in c#
Ex:
I have the start time which is 5/2/2011 10:00:00 Am and set the static Duration to 7 Days
So .... the Time left will be 6 Days 12:00 hour till it reach zero. ends in -> 5/9/2011 10:00:00 Am
Note : The Time Left can not be reset and start over again .. and it depends only
on the Start Time
I don't know how to do this .. any help I will be grateful.
You need a permanent storage (on hard disk) like registry, database or XML file because any time span greater than ~24h is not guaranteed in a in-memory code in ASP.NET.
In case you application reset you need to get the start point from somewhere (permanent storage). Usually when there's no request from clients you application would be unloaded without notice (you loose the in-memory data). Also ASP.NET recycles applications based on some configurations in IIS (application pool you are using for your application).
The .NET 4.0 includes the new System.Threading.CountdownEvent class you may find helpful.
A 7 Day timer in a asp.net page?
Perhaps your approach is wrong. Can you use winforms and install the application as a service instead?
I guess if it has to be a webpage, do the timer in JavaScript (perhaps use JQuery), and when its done, do what you want through JQuery. I.E. call your server side method, or whatever.
If you have trouble with the technical aspects of this:
jQuery countdown timer
Related
I on my PWA application which is always run in one big screen in conference room, I'm displaying records about today planned events, every record contains status and hour when start and end.
Every record have special status like: Finished, InProgress or next.
How find the best approach to determine when records should automatically refresh after end of actual InProgress.
Also, I would like achieve one constant refresh in next day, so that if someone will come to room at the beginning of day, will see meetings for actual day.
I know that I can set refresh every minute, but this not exactly what I want because i don't need this kind of frequently because meetings are from 8 to 16 and one day have just few of events.
Sample:
you can use signalR. where you can create a channel.
if a new record is insert then you can publish it to this channel. and subscribe to your PWA app.
https://dotnet.microsoft.com/en-us/apps/aspnet/signalr.
From your question and your comment above, I understand that there are 3 scenarios:
Schedule an update: You can implement your own scheduler or use a third-party library: There are several options, here some from github (https://github.com/topics/scheduler?l=c%23)
As #Imamul pointed, SignalR, to externally request a change
Update the UI based on a Property change via StateHasChanged() for changes by Finished, InProgress status.
Would these work?
I want to disable a button for twenty minutes this should remain disabled even if the application is restarted.
I was thinking doing this by reading and storing the time when the button is pressed, then read the system time every minute and when the elapsed time be 20 or more minutes, enable the button.
I think this would allow me to restore the time when the button was pressed if the application is restarted; and then check for the elapsed time.
Do you think this is a good idea?
Any other option?
Since you want this setting to work even if the application restarts, this won't be possible unless you involve an external agent. A few options that you might try involve:
Read current Internet time from http://time.windows.com or nist etc and store it somewhere (registry or local file). Use a timer within your application that keeps fetching latest time from the Internet and compares it to the save value. This post allows you to read current Internet time using both HTTP and TCP port 13.
Use Windows scheduled tasks. Set a bool flag somewhere (file/registry again) and ask the scheduled task to clear the flag after 20 minutes. This post should get you going with creating scheduled tasks.
Create a Windows service that keeps running in the background that you could call to set the flag and the length of time for which this flag should remain set. The service should run an internal Timer (and should not rely on system time) to keep track of "ticks". After the specified time has elapsed, the service should clear he flag.
Here is what I am thinking,
When the application starts, disable or hide the button
Set the time stamp when disabling/hiding and store in a database table or a file.
Now read every minute or every five minutes - whichever is convenient to see if 20 minutes have elapsed.
IF 20 minutes have passed, remove the entry from table or file.
And if the application crashes or restarts before setting the time stamp:
1.While setting the time stamp, make sure that the table or file is empty. If not, enable the timer - the time stamp is there.
Well if I were you, I'd basically disable the button
and then start a timer, each minuite, the program should write the time left to a file as well as the current time ( you can also decrypt this file, so that users can't change it )
Now when the program restarts, it should read this file, and start a timer according to the written time in the fil :)
If you want the timer to work even when the program is closed, then you might check this out (File.GetLastAccessTime)
Basically, you should compare the (current time) that has been written to the fil, with the last access time, and make a function to get the time left :)
This might not be a perfect solution, but it will work fine ( I guess )
Kind Regards :)
Please check the image below.
Below I have a simple lottery system where I have these columns,
Vistors : Total no visitors till date
Next DrawTime : Will come from database (no problem with this)
TodayDate : Current IST Date(noproblem with this)
CurrentTime: Current IST time with seconds ticking like clock.
Result
TimeToDraw : Here we have main problem. Here we need is again ticker with minutes and seconds. For example if its get 15mins from db then the field will display 15:00 and on each second it will get decremented like 14:59,14:58... and so on. when the timer reach zero 00:00, i need to execute some server side code that will draw the result based on data saved in db, without reloading the entire page.
I am feeling lost. Please help me.
I have tried few links , but did'nt get any satisfactory result. Here are the links
Call js-function using JQuery timer
http://r4r.co.in/asp.net/01/tutorial/asp.net/How%20to%20make%20CountDown%20Timer%20in%20ASP.NET%20Using%20c-sharp.shtml
Javascript event triggers based on local clock
How to periodically update server-side value on webpage?
Requirements (Update):
Implementation of Minutes:Second(MM:SS) ticker using server side or client side scripts.
How to fire event when the ticker reaches zero(00:00)?
Again after drawing result, ticker will get reset to 15 min/30 min or whatever coming from database.
Please I need purely ASP.Net and C# (if possible) code. Any reference or link will be higly appreciated.
Thanks in Advance
Do NOT poll the server every second, that will create too much unneeded trafic.
Put a timer in the page (javascript) and when the timer reaches 0 poll the server.
The server should keep track of the time by itself; not relying on the client to provide the (potentially spoofed) time and pass back the appropriate response either winning tickets or nothing if it's not time yet to draw.
If you really need/want to push information from a server to the clients I recommend having a look at the suggestions here: https://stackoverflow.com/questions/6883540/http-server-to-client-push-technologies-standards-libraries
For SignalR have a look at this sample it is very close to what you need.
Do NOT use server side code to update the time on webpage unless it is critical.
For the countdown script look at the answer to this question: Javascript Countdown
When the timer reaches zero, refresh the page or execute an Ajax call using jQuery. When the call returns update the page depending on the server's response.
If you need more explicit answers you need to show what you have tried and point out problems you are having.
I am designing a website and it uses Windows Forms (in Visual Studio 10) in which for example i have five-six URLs. Now i am displaying them on home page of my website xyz.com
What i want is, i want to calculate total no. of tweets for all links and display links based on no. of times they are being tweeted/retweeted.
for a url we can calculate no. of tweet using twitter api http://urls.api.twitter.com/1/urls/count.json?url=YourURL
I know all the stuff like receiving JSON values in a string and parsing json to retrieve tweet counts and then compare and display links based on the priority etc.
What i have been using till now it is initiating all the process using a Click_Button.
But i want to know how can i automate this all for each 10 minutes. Its like a end user can see urls priority with just refreshing the page.
One way to do this is to run a scheduled task ever 10 mins which interacts with the DB. The web application also interacts with the DB and thus the two systems are distinct.
Side note: it is strongly recommended to use only console applications as scheduled tasks. If you make a windows form application will will have some issues.
As Kieren Johnstone has pointed out in another answer the best way to do this would be to write a windows service.
I still recommend the solution as described above as a first step since it is easy to debug and test.
Additionally, give some serious consideration to logging and error reporting -- with background tasks you can never know to much about what the heck it was doing when it broke.
If timing itself is not important (it doesn't have to be 10 minutes precisely), I would suggest binding to any event that fires when users use your application. No point in calculating anything if noone is using it :-)
So you could use a login, or page load, or whatever happens at an interval roughly like the interval you wish to achieve.
You can always store a DateTime variable somewhere that you can check to see when the calculation was last made. Something like:
public void MyEventHasFired()
{
DateTime dateLastProcessed = ... //Database? Session data? Anything goes.
if(dateLastProcessed < DateTime.Now.AddMinutes(-10))
{
//calculate
...
dateLastProcessed = DateTime.Now;
}
}
The best solution is definitely a Windows Service. It can be started, stopped and managed well, it's easy to log, maintain..
Scheduled Tasks are very prone to problems. At least in a Windows Service you can configure it to start automatically, re-start if there's a problem, you can control the timing yourself in the code, and catch/handle exceptions as you wish.
The best scheduler i know is Quartz.net
It'is not simple to use but it works great.
You can find an example with asp.net there http://blogs.planetcloud.co.uk/mygreatdiscovery/post/ASPNET-Scheduled-Tasks-with-QuartzNET.aspx
Anyway i agree with Kieren Johnstone: you should use a windows service
I want to read my database get values, change my old message with new value and then show this newly message. I want to do this reading job at everyday 08:00. How can I know if clock is 08:00 AM? Need to control every second? or what else?
The key thing to note here is that you don't actually need to do this at 8am - you need to do it when the application is in use at 8am or the first time the application is in use after 8am - if, that is, you want to do this solely within your ASP.NET application.
You need to track when the message was last updated.
In ASP.NET in a suitable application startup event, call the update method - which should check to see if the value needs updating - and then set a timer (its been 5 years since I wrote the code to do this last, options will have changed) for the "next" 8am - when the timer fires, call your update method, set the timer again. So, if the app starts it will update if required, if its running the update will happen.
If you have more access to the server then create a windows service to do the update.
There are several options in between these two - create a page or a web service to trigger the update logic within the ASP.NET application and then use an external timer to open the page/call the web service at the scheduled time. The most appropriate solution depends on your hosting enviroment and the other resources available to you.
you could create a windows service and schedule it for the time you require every day.