I am trying to make a stopwatch with C# as an exercise for myself. my plan was to make two methods " start()" and " stop()" then call these from my stopwatch class in my main. the problem I have is that I do not know how to get the time span between these two.
for your information, this is how I want the program to work: if they typed s the timer starts and when press enter or type f the time will be shown to them.
this is the code I have written so far, but got stuck when getting the time span.
class StopWatch
{
DateTime starting = DateTime.Now;
DateTime finishing = DateTime.Now;
public void start()
{
Console.WriteLine(starting);
}
public void stop()
{
Console.WriteLine(finishing);
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("type s to start and f to stop");
var input = Console.ReadLine();
var stopwatch = new StopWatch();
if (input.ToLower() == "s") { stopwatch.start(); }
var Input2 = Console.ReadLine();
if (Input2.ToLower() == "f") { stopwatch.stop(); }
Console.ReadKey();
}
}
I agree with the comment to use what already exists in the library, but since you said you are doing this as an exercise, here is some feedback:
To answer you direct question how to get a TimeSpan:
var duration = finishing - starting;
The current implementation will not do what you intend to do, since you set both starting and finishing at object creation time: field initializers are executed before any constructor code. So you should set starting in the start() method and finishing in the stop() method. Then you can calculate the duration as shown above in the stop() method, too.
And allow me a little side note on naming: "starting" and "finishing" are progressive forms in English, but here you want to name specific values. Therefore I'd recommend "startTime" and "endTime"/"stopTime".
Related
According to MSDN, the Stopwatch class instance methods aren't safe for multithreaded access. This can also be confirmed by inspecting individual methods.
However, since I only need simple "time elapsed" timers at several places in my code, I was wondering if it could still be done lockless, using something like:
public class ElapsedTimer : IElapsedTimer
{
/// Shared (static) stopwatch instance.
static readonly Stopwatch _stopwatch = Stopwatch.StartNew();
/// Stopwatch offset captured at last call to Reset
long _lastResetTime;
/// Each instance is immediately reset when created
public ElapsedTimer()
{
Reset();
}
/// Resets this instance.
public void Reset()
{
Interlocked.Exchange(ref _lastResetTime, _stopwatch.ElapsedMilliseconds);
}
/// Seconds elapsed since last reset.
public double SecondsElapsed
{
get
{
var resetTime = Interlocked.Read(ref _lastResetTime);
return (_stopwatch.ElapsedMilliseconds - resetTime) / 1000.0;
}
}
}
Since _stopwatch.ElapsedMilliseconds is basically a call to QueryPerformanceCounter, I am presuming it's safe to be called from multiple threads? The difference with a regular Stopwatch is that this class is basically running all the time, so I don't need to keep any additonal state ("running" or "stopped"), like the Stopwatch does.
(Update)
After the suggestion made by #Scott in the answer below, I realized that Stopwatch provides a simple static GetTimestamp methods, which returns raw QueryPerformanceCounter ticks. In other words, the code can be modified to this, which is thread safe:
public class ElapsedTimer : IElapsedTimer
{
static double Frequency = (double)Stopwatch.Frequency;
/// Stopwatch offset for last reset
long _lastResetTime;
public ElapsedTimer()
{
Reset();
}
/// Resets this instance.
public void Reset()
{
// must keep in mind that GetTimestamp ticks are NOT DateTime ticks
// (i.e. they must be divided by Stopwatch.Frequency to get seconds,
// and Stopwatch.Frequency is hw dependent)
Interlocked.Exchange(ref _lastResetTime, Stopwatch.GetTimestamp());
}
/// Seconds elapsed since last reset
public double SecondsElapsed
{
get
{
var resetTime = Interlocked.Read(ref _lastResetTime);
return (Stopwatch.GetTimestamp() - resetTime) / Frequency;
}
}
}
The idea of this code, to clarify, is:
to have a simple and fast way of checking if time has elapsed since a certain operation/event,
methods should not corrupt state if called from multiple threads,
must be insensitive to OS clock changes (user changes, NTP sync, time zone, etc.)
I would use it similar to this:
private readonly ElapsedTimer _lastCommandReceiveTime = new ElapsedTimer();
// can be invoked by multiple threads (usually threadpool)
void Port_CommandReceived(Cmd command)
{
_lastCommandReceiveTime.Reset();
}
// also can be run from multiple threads
void DoStuff()
{
if (_lastCommandReceiveTime.SecondsElapsed > 10)
{
// must do something
}
}
The only change I would suggest is use Interlocked.Exchange(ref _lastResetTime, _stopwatch.ElapsedTicks); instead of Milliseconds because if you are in high performance mode it is possible to get sub millisecond results from QueryPerformanceCounter.
I would recommend creating multiple instances of the Stopwatch and only reading from it on the same thread.
I don't know what your asynchronous code looks like but in psuedo code I would do either:
Stopwatch watch = Stopwatch.Startnew();
DoAsyncWork((err, result) =>
{
Console.WriteLine("Time Elapsed:" + (watch.ElapsedMilliseconds / 1000.0));
// process results...
});
Or:
public DoAsyncWork(callback) // called asynchronously
{
Stopwatch watch = Stopwatch.Startnew();
// do work
var time = watch.ElapsedMilliseconds / 1000.0;
callback(null, new { time: time });
}
The first example assumes that DoAsyncWork work does the work in a different thread then calls the callback when completed, marshalling back to the callers thread.
The second example assumes the caller is handling the threading and this function does all of the timing itself, passing the result back to the caller.
I am making a C# aplication. I have got one main process for verifying data from a database. A timer checks every 100 ticks if I have user input. After I get user input my main process continues (it waits for userinput at the start). After verifying and doing multiple things the method is done. The thing is, I would like it to go to the beginning again waiting for the next bit of input.
I was thinking of calling the method again at every possible end of the process. I have a feeling that this will create a resource heavy program though (not the worst thing, but better no bad habits than a few right?).
Example:
bool cont = false;
public void process()
{
while (cont == false) {;}
//Various sorting criteria that all end up with cont = false; process(), the userinput has been processed.
}
timer1 tick event
{
if (userinput)
cont = true;
}
As you don't saw how you will get the user input, i don't implemented this one. But the main logic to your question is :
class MainClass
{
public static void Main()
{
MyRecursiveFunction();
AfterUserInput();
}
public static void MyRecursiveFunction()
{
if (userinput)
{ return; }
// Waits 100 ticks to check again
Thread.Sleep(new TimeSpan(100));
MyRecursiveFunction();
}
public static void AfterUserInput()
{
// All that you need to do after the user input
}
}
I have made a C# alarm clock and it's working fine. the problem is that when it runs it consumes 20% of the processor (on an i5 2410M processor) what should I do?
here is my code:
using System;
namespace assigment1
{
class Program
{
static void Main(string[] args)
{
DateTime uptime = new DateTime (2013,12,10,4,0,0);
Console.WriteLine("This alarm is set to go off at 4:00 am");
while (true)
{
if (DateTime.Now.Minute == uptime.Minute && DateTime.Now.Hour == uptime.Hour)
{
for (int j = 1000; j < 22767; j++)
{
Console.Beep(j, 500);
Console.Write("Wake up! it is {0}:{1} already! ", DateTime.Now.Hour, DateTime.Now.Minute);
}
}
}
}
}
}
This is because your while loop is running continuously without any break. Add a Thread.Sleep. This will add a pause in between checks and greatly increase your performance:
class Program
{
static void Main(string[] args)
{
DateTime uptime = new DateTime (2013,12,10,4,0,0);
Console.WriteLine("This alarm is set to go off at 4:00 am");
while (true)
{
if (DateTime.Now.Minute == uptime.Minute && DateTime.Now.Hour == uptime.Hour)
{
for (int j = 1000; j < 22767; j++)
{
Console.Beep(j, 500);
Console.Write("Wake up! it is {0}:{1} already! ", DateTime.Now.Hour, DateTime.Now.Minute);
}
}
Thread.Sleep(1500); // Sleep 1.5 seconds.
}
}
}
You need to calculate the time till till the alarm should beep and use the timer class. Just set the interval to the time remaining till alarm and stop the timer after that. Something like this should work
DateTime alarmTime = new DateTime(2013,12,10,4,0,0);
System.Windows.Forms.Timer alarmTimer = new System.Windows.Forms.Timer();
alarmTimer.Interval = (alarmTime - DateTime.Now).Milliseconds;
alarmTimer.Tick += alarmTimer_Tick;
alarmTimer.Start();
your event
void alarmTimer_Tick(object sender, EventArgs e)
{
alarmTimer.Stop();
Console.Write("Wake up! it is {0}:{1} already! ", DateTime.Now.Hour, DateTime.Now.Minute);
}
if you want an alarm clock why you don't use Timer Class
I don't know if you can do that, but you can change the thread priority of the executing thread via the Priority property. You may want to try the following:
Thread.CurrentThread.Priority = ThreadPriority.Lowest;
Also, I don't think you really want to cap it. If the machine is otherwise idle, you'd like it to get busy on with the task, right? ThreadPriority helps communicate this to the scheduler.
You are putting the check within a while loop, which means it will be utilising a large proportion of your processor time.
I would suggest having a look at this article (http://www.infolet.org/2012/11/create-digital-clock-on-c-sharp-program-code.html) which describes how to do this using the Timer Class.
UPDATE:
This SO answer is pretty nice and may be more suited if you're happy to use events;
https://stackoverflow.com/a/1493235/465404
I think you should definitely be using a Timer class for your alarm and just change the tick interval accordingly. This will easily allow you to manage recurrence of the alarm as well.
So you're interval will be the difference in time between when the alarm is set and when you want it to go off.
I have used multiple of these running concurrently in a Win Forms app with very small resource utilisation.
I was tasked to append a timer within my working console app to let it close automatically after some time without requiring a user input.
My application has functions that delete certain data in the database based on some conditions and exits everytime the user inputs 'exit'.
Now the requirement is to automatically stop the process even if the deleting of items is not yet done given that the time set to close the application is provided, say 2 hours.
Can someone help me?
Thanks.
You can create a System.Timers.Timer with an interval of TimeSpan.FromHours(2) and call Environment.Exit() in its Elapsed handler.
I don't believe that killing the program is a good idea since you are manipulating data in a database but I think would be the simplest way to do it.
using Timer = System.Threading.Timer;
class Program
{
private static readonly Timer _timer =
new Timer(o => Environment.Exit(0), null, 5000, Timeout.Infinite);
static void Main(string[] args)
{
Console.ReadLine();
}
}
1) create a timer
2) set interval and the elapsed event handler
3) enable the timer for run
when timer triggers in the method hooked to the event just exit the application
If you are deleting data from database then stopping it Abruptly could be catastrophic. So you can implement somthing like this.
Perform the time consuming operation in a BackGroundWorker
Implement a Timer as explained in other examples.
Then when the Tick/Interval Event is raised Request the BackgroundWorker to Cancel the Task.
In your Do Work code Listen for this Cancel request and stop the deletion process safely(Either the Do Deletion or Don't Perform the it At all)
Then Use Environment.Exit() to exit out of the program.
Hope it helps
I solved this problem by having an app.config file where there is a value for key="Stoptime".
I then added a condition that checks the current time against the set end time. Following is an example solution (for those having the same problem):
public static void Main(string[] args)
{
string stoptime = ConfigurationManager.AppSettings["Stoptime"];
DateTime timeEnd = Convert.ToDateTime(stoptime);
today = DateTime.Now;
Console.WriteLine(today);
for (int i = 0; i < 100000; i++)
{
id.Add(i.ToString());
}
foreach(string item in id)
{
today = DateTime.Now;
if (timeEnd.CompareTo(today) >= 0)
{
Console.CursorLeft = 0;
Console.Write(item + " " + today);
}
else
{
Console.WriteLine();
Console.WriteLine("break.");
break;
}
}
Console.ReadKey();
}
Is there any clever method out there to make my executeEveryDayMethod() execute once a day, without having to involve the Windows TaskScheduler?
I achieved this by doing the following...
Set up a timer that fires every 20 minutes (although the actual timing is up to you - I needed to run on several occasions throughout the day).
on each Tick event, check the system time. Compare the time to the scheduled run time for your method.
If the current time is less than the scheduled time, check a in some persistent storage to get the datetime value of the last time the method ran.
If the method last ran more than 24 hours ago, run the method, and stash the datetime of this run back to your data store
If the method last ran within the last 24 hours, ignore it.
HTH
*edit - code sample in C# :: Note : untested...
using System;
using System.Collections.Generic;
using System.Text;
using System.Timers;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Timer t1 = new Timer();
t1.Interval = (1000 * 60 * 20); // 20 minutes...
t1.Elapsed += new ElapsedEventHandler(t1_Elapsed);
t1.AutoReset = true;
t1.Start();
Console.ReadLine();
}
static void t1_Elapsed(object sender, ElapsedEventArgs e)
{
DateTime scheduledRun = DateTime.Today.AddHours(3); // runs today at 3am.
System.IO.FileInfo lastTime = new System.IO.FileInfo(#"C:\lastRunTime.txt");
DateTime lastRan = lastTime.LastWriteTime;
if (DateTime.Now > scheduledRun)
{
TimeSpan sinceLastRun = DateTime.Now - lastRan;
if (sinceLastRun.Hours > 23)
{
doStuff();
// Don't forget to update the file modification date here!!!
}
}
}
static void doStuff()
{
Console.WriteLine("Running the method!");
}
}
}
Take a look at quartz.net. It is a scheduling library for .net.
More specifically take a look here.
If the time when it is run is not relevant and can be reset each time the program starts you can just set a timer, which is the easiest thing to do. If that's not acceptable it starts getting more complex, like the solution presented here and which still doesn't solve the persistence problem, you need to tackle that separately if you truly wish to do what Scheduled Tasks would. I'd really consider again if it's worth going through all the trouble to replicate a perfectly good existing functionality.
Here's a related question (Example taken from there).
using System;
using System.Timers;
public class Timer1
{
private static Timer aTimer = new System.Timers.Timer(24*60*60*1000);
public static void Main()
{
aTimer.Elapsed += new ElapsedEventHandler(ExecuteEveryDayMethod);
aTimer.Enabled = true;
Console.WriteLine("Press the Enter key to exit the program.");
Console.ReadLine();
}
// Specify what you want to happen when the Elapsed event is
// raised.
private static void ExecuteEveryDayMethod(object source, ElapsedEventArgs e)
{
Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);
}
}
public partial class Main : Form
{
public Main( ) // Windows Form is called Main
{
InitializeComponent( );
}
private void Main_Load( object sender, EventArgs e )
{
/*
This example uses a System.Windows.Forms Timer
This code allows you to schedule an event at any given time in one day.
In this example the timer will tick at 3AM.
*/
Int32 alarm = GetAlarmInMilliseconds( 3, 0, 0 ); // Milliseconds until 3:00 am.
timer_MessageCount.Interval = alarm; // Timer will tick at 3:00am.
timer_MessageCount.Start( );
}
private Int32 GetAlarmInMilliseconds(Int32 eventHour, Int32 eventMinute, Int32 eventSecond )
{
DateTime now = DateTime.Now;
DateTime eventTime = new DateTime( now.Year, now.Month, now.Day, eventHour, eventMinute, eventSecond );
TimeSpan ts;
if ( eventTime > now )
{
ts = eventTime - now;
}
else
{
eventTime = eventTime.AddDays( 1 );
ts = eventTime - now;
}
Console.WriteLine("Next alarm in: {0}", ts );
return ( Int32 ) ts.TotalMilliseconds;
}
static void DoSomething( )
{
Console.WriteLine( "Run your code here." );
}
private void timer_MessageCount_Tick( object sender, EventArgs e )
{
DoSomething( );
Int32 alarm = GetAlarmInMilliseconds( 3, 0, 0 ); // Next alarm time = 3AM
timer_MessageCount.Interval = alarm;
}
}
Suppose you have the daily time in _Settings.DataCleanupTime in Format "hh:mm:ss"
//note the namespace, there are 4 different timers in .NET
System.Threading.Timer _Timer;
DateTime now = DateTime.Now;
//convert "hh:mm:ss" to three integers
var dateparts = _Settings.DataCleanupTime.Split(new char[] { ':' }).Select(p => Convert.ToInt32(p)).ToArray();
DateTime firstTime = new DateTime(now.Year, now.Month, now.Day, dateparts[0], dateparts[1], dateparts[2]);
//e.g. firsttime is today at 2am and it is already 6am
if(firstTime < now)
{
//first run will be tomorrow
firstTime = firstTime.AddDays(1);
}
int delay = Convert.ToInt32((firstTime - now).TotalMilliseconds);
_Timer = new Timer(DoWork, state:null, delay, 3600 * 24 * 1000);
The signature of DoWork is:
public void DoWork(Object state)
To stop the timer just call:
_Timer.Dispose();
You could query time and run if your within some time frame, that way even if the machine goes off you'll call the method or use a timer like Vinko's suggestion.
But the better solution (akin to older CRON versions, so its a proven pattern) is to have some persistent data, with the cheapest solution I can think of right now being a blank file, check its last modified attribute, and if it hasn't been modified within the last 24 hours you touch it and run your method. This way you assure the method gets run first thing in the case the application is out for the weekend for example.
I've done this in C# before, but its was a year ago at another Job, so I don't have the code but it was about 20 lines (with comments and all) or so.
To run the job once daily between 7 and 8pm, i set up a timer with interval = 3600000 ms and then just execute the following code for timer tick.
private void timer1_Tick(object sender, EventArgs e)
{
//ensure that it is running between 7-8pm daily.
if (DateTime.Now.Hour == 19)
{
RunJob();
}
}
An hour window is fine for me. Extra granularity on time will require a smaller interval on the timer (60000 for a minute) and including minutes on the if.
eg
{
//ensure that it is running at 7:30pm daily.
if (DateTime.Now.Hour == 19 && DateTime.Now.Minute == 30)
{
RunJob();
}
}
If you only want to run it once a day and don't care when, this will work (will run just after midnight).
Declare a DateTime variable:
DateTime _DateLastRun;
In your startup, set the initial date value:
_DateLastRun = DateTime.Now.Date;
In the logic area where you want to check whether to perform the action:
if (_DateLastRun < DateTime.Now.Date)
{
// Perform your action
_DateLastRun= DateTime.Now.Date;
}
You can try this solution.
public Main()
{
StartService();
}
public async Task StartService(CancellationToken token = default(CancellationToken))
{
while (!token.IsCancellationRequested)
{
ExecuteFunction();
try
{
await Task.Delay(TimeSpan.FromDays(1), token);
}
catch (TaskCanceledException)
{
break;
}
}
}
public async Task ExecuteFunction()
{
...
}
Here is how you can do it if you're running a Windows Forms Application. But you need to configure a setting so that you can store the last date the event was fired. If you never intend to close the app you can just store the date as a static value.
Im using a timer to fire the event, as following:
private void tmrAutoBAK_Tick(object sender, EventArgs e)
{
if (BakDB.Properties.Settings.Default.lastFireDate != DateTime.Now.ToString("yyyy-MM-dd"))
{
tmrAutoBAK.Stop(); //STOPS THE TIMER IN CASE OF EVENTUAL MESSAGEBOXES.
createBakup(); //EVENT
BakDB.Properties.Settings.Default.lastFireDate = DateTime.Now.ToString("yyyy-MM-dd"); //STORING CURRENT DATE TO SETTINGS FILE.
BakDB.Properties.Settings.Default.Save(); //SAVING THE SETTING FILE.
tmrAutoBAK.Start(); //RESTARTING TIMER
}
}
This is my very simple solution to execute a method once once a day:
private static DateTime _LastAccessedTime;
private static void OnceADayCode() // method that you want access once a day
{
_LastAccessedTime = DateTime.Today;
}
public static void PublicMethod() // this can be a method called from outside
{
if (_LastAccessedTime != DateTime.Today)
{
OnceADayCode();
}
}
with the same logic you can also use:
private static DateTime _LastAccessedTime;
private static void OnceADayCode()
{
if (_LastAccessedTime.Today != DateTime.Today)
{
// code that you want access once a day
_LastAccessedTime = DateTime.Today;
}
}