How to compare time in c#? - c#

for (int i = 0; i <= 10; i++)
{
if (5 seconds have passed)
{
do something;
}
}
How can I check if 5 seconds have passed? If 5 seconds have passed for example, it "does something" , then again if more 5 seconds have passed, it does the same thing again, and so on.
And important: If I should use date & time to do it, it should not be a specific time, it should be automatic.

You need a reference start date and a "now" date;
if(startDate.addSeconds(5) < DateTime.Now) do something
If I understood well the situation could be like:
DateTime startDate = DateTime.Now;
for (int i = 0; i <= 10; i++)
{
//do something that can take a long time
//...
//..
if(startDate.addSeconds(5) < DateTime.Now) //5 seconds have passed
{
//do something else
}
}

Thread.Sleep(5000); But this will block your application. You could use a Timer.
For .NET 4.5 you could use the await Task.Delay(5000);
I think FelipeP made a great statement..
But the code you gave, and the description of the problem aren't the same.
The code says, if time is passed.
Your description says the loop must wait until the time is passed.
Update:
System.Windows.Forms.Timer _timer = new System.Windows.Forms.Timer();
void CreateTimer()
{
_timer = new System.Windows.Forms.Timer();
_timer.Tick += new EventHandler(_timer_Tick);
_timer.Interval = 5000;
_timer.Enabled = true;
}
void _timer_Tick(object sender, EventArgs e)
{
// do something.
}

Related

How to use a timer to see how long an equation takes in C#

In my program I'm trying to see how long it will take to calculate an amount of prime numbers
double time;
private void button1_Click(object sender, EventArgs e)
{
bool isPrime = true;
timer1.Start();
for (int i = 2; i <= 200000; i++)
{
for (int j = 2; j <= 200000; j++)
{
if (i != j && i % j == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
{
//listBox1.Items.Add(i);
}
isPrime = true;
}
timer1.Stop();
MessageBox.Show(time.ToString() + "ms");
}
private void timer1_Tick(object sender, EventArgs e)
{
time += 0.001; ;
}
However when the program finishes calculating the messagebox displays time as zero.
I tested the program using the stopwatch class and that works, however I was wondering is there any way to use a timer instead?
Thank you!
If this is purely academic:
myTimer.Interval = 1;
myTimer.Elapsed += IncrementEvent;
You could do the above to fire an event on each increment.
Causing the event to be fired every ~1ms.
public int incrementCounter {get; set; }
public void IncrementEvent()
{
incrementCounter++;
}
I also believe you'd need to wire this up for multi-threading. (I can't remember the last time I used a timer, and never in a tight loop where locking would be a concern.
and then obviously use that value in your message box:
MessageBox.Show(incrementCounter.ToString() + "ms");
However, I'd argue to never use anything like this in production.
You don't have much precision. 1ms is the smallest unit of time.
The timer isn't very reliable. Even running it every few seconds, you'll see milliseconds of the time executed vary slightly.
A timer is meant to schedule events to happen once t(ime) has ellapsed. There are much better tools for measuring how long an event takes.

How to get Timespan in Milliseconds

I want to get the timespan in milliseconds by comparing two timestamps with DateTime.Now and the previous DateTime. I want to check if there is an event every 10 Milliseconds or later but the totalmilliseconds from DeltaT is like 188 or so. It is too high than I am expecting that is why I think there must be somethimg wrong. Or does everything look alright?
DateTime timestamp;
DateTime timestampAlt;
TimeSpan deltaT;
public void OnSensorChanged(SensorEvent e)
{
timestamp = System.DateTime.Now;
deltaT = timestamp - timestampAlt;
if (deltaT.TotalSeconds <= 0.01)
{
return;
}
UPDATE:
I really appreciate all of you answers but I think there is a misunderstanding (my mistake sry). So here again:
Whenever the listener recognizes an event, I want to save the timestamp and compare with the timespamp of the event before. If there is a gap of more than 10 Milliseconds between the 2 events, then I do want to know more about this new event. If not, I dont even want to continue and will leave by a return.
public void OnSensorChanged(SensorEvent e)
{
timestamp = System.DateTime.Now;
deltaT = timestamp - timestampAlt;
//int deltaT2 = timestamp.Millisecond - timestampAlt.Millisecond;
String timestampStr = timestamp.ToString("ff");
String timestampStrA = timestampAlt.ToString("ff");
if (deltaT.TotalMilliseconds <= 10 || deltaT.TotalMilliseconds <= -10) //deltaT.Seconds <= 0.01
{
return;
}
timestampAlt = timestamp;
newValue = e.Values[2];
//if (buffer[99] != 0.00)
// if last element of list is empty, add elements to buffer
if (buffer.Count <=99)
{
buffer.Add(newValue);
zeitbuffer.Add(timestamp);
}
else
{
Ableitung(DeltaBuffer(), DeltaTime()); // if last index of list is filled, do that function
}
if (e.Values[2] >= 11)
{
try
{
lock (_syncLock)
{
String z2 = newValue.ToString("0.0");
//noteInt2 = Convert.ToInt32(newValue);
try
{
_sensorTextView2.Text = string.Format("Note: {0}", z2 );
eventcounter.Add(z2);
You can use deltaT.TotalMilliseconds which expresses your delta in milliseconds. Therefore your check could be rewritten as
if (deltaT.TotalMilliseconds <= 10)
{
return;
}
10 is a value I inferred. It might not be what you need, but your question is partial. This answer addresses your particular question, however if you need to measure the duration of a task you should use the Stopwatch class, which is designed for that purpose.
if you want to fire an event every n-Seconds you can use a timer that fires an event when he elapses:
Timer timer = new Timer();
timer.Interval = 100;
timer.Elapsed += YourAmasingEvent;
timer.Start();
private void YourAmasingEvent(object sender, ElapsedEventArgs e)
{
//do something here
(sender as Timer).Start();
}
Using your code:
I guess you want to wait until the time elapsed in this case you would have to use a loop like this:
timestamp = System.DateTime.Now;
deltaT = timestamp - timestampAlt;
while(true)
{
if (deltaT.TotalSeconds <= 0.01)
{
return;
}
timestamp = System.DateTime.Now;
deltaT = timestamp - timestampAlt;
}

Windows fourm crashes after clicking button

I decided to create a basic timer just for my use and everytime i click the start button the program freezes up completely. Im i missing something obvious or?
namespace Timer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn_Start_Click(object sender, EventArgs e)
{
int secs = 0, mins = 0, hours = 0;
for (int num1 = 1; num1 > 0;)
{
txt_secs.Text = secs.ToString() ;
System.Threading.Thread.Sleep(1000);
secs = secs + 1;
if (secs == 60)
{
secs = 0;
mins = mins + 1;
txt_mins.Text = mins.ToString();
}
if (mins == 60)
{
mins = 0;
hours = hours + 1;
txt_hours.Text = hours.ToString();
}
}
}
}
}
The issue is in System.Threading.Sleep()
Per Microsoft Documentation your thread will be suspended. The thread is in an infinite loop and thus will be (essentially) permanently suspended.
Consider using Timers or the Stopwatch Class or (if you need threading) System.Threading.Timer Instead

Multiple countdown timer in listview C# duplicating

I'm new to C#.
I'm trying to make a simple task reminder program.
The problem is, when I try to add a countdown for deadline time, it won't work correctly.
My first task countdown will be overwritten by my second task countdown, the same case when I add the third task and so on.
Here is the code of the correlating part.
private void buttonSave_Click(object sender, EventArgs e)
{
if (this.textBox_Task.Text != "")
{
listView1.View = View.Details;
ListViewItem lvwItem = listView1.Items.Add(dateTimePicker1.Text);
var day = dateTimePicker1.Value.Day;
var month = dateTimePicker1.Value.Month;
var year = dateTimePicker1.Value.Year;
endTime = new DateTime(year,month,day);
//Console.WriteLine(day);
//Console.WriteLine(month);
//Console.WriteLine(year);
//Console.WriteLine(dTime
Timer t = new Timer();
t.Interval = 500;
t.Tick += new EventHandler(t_Tick);
t.Start();
lvwItem.SubItems.Add(textBox_Task.Text);
lvwItem.SubItems.Add(textBox_Note.Text);
lvwItem.SubItems.Add("");
this.dateTimePicker1.Focus();
this.textBox_Note.Focus();
this.textBox_Task.Focus();
this.textBox_Task.Clear();
this.textBox_Note.Clear();
}
else
{
MessageBox.Show("Please enter a task to add.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.textBox_Task.Clear();
this.textBox_Note.Clear();
}
}
void t_Tick(object sender, EventArgs e)
{
TimeSpan ts = endTime.Subtract(DateTime.Now);
var hari = dateTimePicker1.Value.Day;
Console.WriteLine(ts.Days);
for (int i = 0; i < listView1.Items.Count; i++)
{
if (ts.Days == 0)
{
listView1.Items[i].SubItems[3].Text = "DEADLINE";
}
else
{
listView1.Items[i].SubItems[3].Text = ts.ToString("d' Days 'h' Hours 'm' Minutes 's' Seconds to go'");
}
}
}
It would be much appreciated for anyone who willing to help.
Thanks in advance.
Here is the link to the picture of my problem
What you are doing now is on each button click override the current endTime object by a new one like:
endTime = new DateTime(year,month,day);
If you assign a new DateTime object to endTime. You override the old one. So the first button click will work but the second will create a new object of DateTime and assign it to endTime. Next you are calculating the time difference on that one object DateTime. So it is logic that it will be the same time for each listview items
If you want to have more than one DateTime use a List to store it in like
List<DateTime> _times = new List<DateTime>();
In the button click method add the DateTime to the list
// here add the datetime to the list
DateTime dateTime = new DateTime(year, month, day);
_times.Add(dateTime);
Next you can loop thru the dates and calculate for each one the time difference in the tick method:
foreach (var dateTime in _times)
{
TimeSpan ts = dateTime.Subtract(DateTime.Now);
// etc..
}
Also you are creating a timer for each time to calculate after 500 ms. You now can use one timer this is more efficient than crating one for each time. Just assign this in the constructor
public Form1()
{
InitializeComponent();
Timer t = new Timer();
t.Interval = 500;
t.Tick += new EventHandler(t_Tick);
t.Start();
}
Whole code
public partial class Form1 : Form
{
// This is the list where the DateTimes are stored so you can have more values
List<DateTime> _times = new List<DateTime>();
public Form1()
{
InitializeComponent();
// Assign the timer here
Timer t = new Timer();
t.Interval = 500;
t.Tick += new EventHandler(t_Tick);
t.Start();
}
private void buttonSave_Click(object sender, EventArgs e)
{
if (this.textBox_Task.Text != "")
{
listView1.View = View.Details;
ListViewItem lvwItem = listView1.Items.Add(dateTimePicker1.Text);
var day = dateTimePicker1.Value.Day;
var month = dateTimePicker1.Value.Month;
var year = dateTimePicker1.Value.Year;
// Add Datetime to list
DateTime dateTime = new DateTime(year, month, day);
_times.Add(dateTime);
lvwItem.SubItems.Add(textBox_Task.Text);
lvwItem.SubItems.Add(textBox_Note.Text);
lvwItem.SubItems.Add("");
this.dateTimePicker1.Focus();
this.textBox_Note.Focus();
this.textBox_Task.Focus();
this.textBox_Task.Clear();
this.textBox_Note.Clear();
}
else
{
MessageBox.Show("Please enter a task to add.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.textBox_Task.Clear();
this.textBox_Note.Clear();
}
}
void t_Tick(object sender, EventArgs e)
{
// loop thru all datetimes and calculate the diffrence
foreach (var dateTime in _times)
{
// Call the specific date and subtract on it
TimeSpan ts = dateTime.Subtract(DateTime.Now);
var hari = dateTimePicker1.Value.Day;
Console.WriteLine(ts.Days);
for (int i = 0; i < listView1.Items.Count; i++)
{
if (ts.Days == 0)
{
listView1.Items[i].SubItems[3].Text = "DEADLINE";
}
else
{
listView1.Items[i].SubItems[3].Text = ts.ToString("d' Days 'h' Hours 'm' Minutes 's' Seconds to go'");
}
}
}
}
}

Only execute method after X number of seconds since last use

I have a foreach statement, and I need to make it so that a method called at the end of the foreach and if statements only executes after 3 seconds from it's last execution time.
Here's the code.
//Go over Array for each id in allItems
foreach (int id in allItems)
{
if (offered > 0 && itemsAdded != (offered * 3) && tDown)
{
List<Inventory.Item> items = Trade.MyInventory.GetItemsByDefindex(id);
foreach (Inventory.Item item in items)
{
if (!Trade.myOfferedItems.ContainsValue(item.Id))
{
//Code to only execute if x seconds have passed since it last executed.
if (Trade.AddItem(item.Id))
{
itemsAdded++;
break;
}
//end code delay execution
}
}
}
}
And I don't want to Sleep it, since when an item is added, I need to get confirmation from the server that the item has been added.
How about a simple time comparison?
var lastExecution = DateTime.Now;
if((DateTime.Now - lastExecution).TotalSeconds >= 3)
...
You could save 'lastExecution' in your Trade-Class. Of course, the code block is not called (items are not added) with this solution if the 3 seconds haven't elapsed.
--//---------------------------------
Different solution with a timer: we add programmatically a Windows Forms timer component. But you will end in Programmers Hell if you use this solution ;-)
//declare the timer
private static System.Windows.Forms.Timer tmr = new System.Windows.Forms.Timer();
//adds the event and event handler (=the method that will be called)
//call this line only call once
tmr.Tick += new EventHandler(TimerEventProcessor);
//call the following line once (unless you want to change the time)
tmr.Interval = 3000; //sets timer to 3000 milliseconds = 3 seconds
//call this line every time you need a 'timeout'
tmr.Start(); //start timer
//called by timer
private static void TimerEventProcessor(Object myObject, EventArgs myEventArgs)
{
Console.WriteLine("3 seconds elapsed. disabling timer");
tmr.Stop(); //disable timer
}
DateTime? lastCallDate = null;
foreach (int id in allItems)
{
if (offered > 0 && itemsAdded != (offered * 3) && tDown)
{
List<Inventory.Item> items = Trade.MyInventory.GetItemsByDefindex(id);
foreach (Inventory.Item item in items)
{
if (!Trade.myOfferedItems.ContainsValue(item.Id))
{
//execute if 3 seconds have passed since it last execution...
bool wasExecuted = false;
while (!wasExecuted)
{
if (lastCallDate == null || lastCallDate.Value.AddSeconds(3) < DateTime.Now)
{
lastCallDate = DateTime.Now;
if (Trade.AddItem(item.Id))
{
itemsAdded++;
break;
}
wasExecuted = true;
}
System.Threading.Thread.Sleep(100);
}
}
}
}
}

Categories

Resources