2

I need the difference of two datetime down to the millisecond comparison the first datetime is going to be less than the second, its to stop a loop in the gridviews delete event

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    if (!Ok2Delete(e.RowIndex)) return;    

    // your logic goes here and the above IF statement 
    // will hopefully guarantee your code to run once.
}
private bool Ok2Delete(int ri) // ri is the record index to be deleted
{
    if (Session["ri"] == null ||
        (!((ri == ((int)Session["ri"])) &&
        (DateTime.Now.Subtract((DateTime)Session["ri_time_stamp"]).Seconds < 2))))
    {
        Session["ri"] = ri;
        Session["ri_time_stamp"] = DateTime.Now;
        return true;
    }
    return false;
}

this code isn't working as expected

ONYX
  • 5,679
  • 15
  • 83
  • 146
  • Your code is looking for more than a 2 second difference in the OK2Delete function. What exactly are you looking for instead? More than 0 milliseconds? – scott.korin Jan 10 '12 at 04:13
  • 1
    Also please use DateTime.UtcNow instead of DateTime.Now. It makes your app independent of machine & TimeZone. – Amar Palsapure Jan 10 '12 at 04:17
  • Well everyone made an effort so i need to choose an answer but it didn't quite work – ONYX Jan 11 '12 at 20:39

3 Answers3

2

You will want something like this (using TotalMilliseconds of the TimeSpan that is the result of subtracting the two datetime objects):

DateTime dt1 = DateTime.Now;
DateTime dt2 = DateTime.Now;

Console.WriteLine(dt2.Subtract(dt1).TotalMilliseconds.ToString());

For your specific scenario:

DateTime.Now.Subtract((DateTime)Session["ri_time_stamp"]).TotalMilliseconds < 500

Update

Based on comments and review of the code, the issue is not related to time difference. Instead the problem is in the RowDeleting code. The following line:

if (!Ok2Delete(e.RowIndex)) return;  

should be changed to

if (!Ok2Delete(e.RowIndex)) {
    e.Cancel = true; 
    return;
}  
competent_tech
  • 44,465
  • 11
  • 90
  • 113
  • I think your logic may need some tweaking. Right now your logic is: if there was no previous ri in the session OR (the same ri is not in the session OR (the same ri is in the session AND the elapsed time < 2 seconds)). Is this the logic you are after? – competent_tech Jan 10 '12 at 04:08
  • This is some code on asp.net forums dealing with the problem of the delete event firing twice – ONYX Jan 10 '12 at 04:11
  • Ah, the problem is that if it is not ok to delete, you need to set `e.Cancel = True` to prevent the deletion (in the RowDeleting event). I have updated the answer with what you need to do. – competent_tech Jan 10 '12 at 04:12
2

(end - start).TotalMilliseconds, where start and end are DateTime

Be aware that you may not be able to get millisecond precision out of DateTime.Now(). See this question. You can use a Stopwatch if you need finer precision.

Community
  • 1
  • 1
geofftnz
  • 9,954
  • 2
  • 42
  • 50
2

Use .TotalSeconds... otherwise a TimeSpan of 122 TotalSeconds will be 2 seconds.

private bool Ok2Delete(int ri) // ri is the record index to be deleted
{
    if (Session["ri"] == null ||
       (!((ri == ((int)Session["ri"])) &&
       (DateTime.Now.Subtract((DateTime)Session["ri_time_stamp"]).TotalSeconds < 2))))
    {
       Session["ri"] = ri;
       Session["ri_time_stamp"] = DateTime.Now;
       return true;
    }
    return false;
}
agent-j
  • 27,335
  • 5
  • 52
  • 79
  • You should use "TotalMilliseconds" as the question wanted time down to the millisecond. – Sprunth Jan 10 '12 at 04:05
  • @Sprunth, I think the OP means for the comparison to be 'accurate' to the millisecond, not the value being compared... it will be even more accurate than milliseconds. – agent-j Jan 10 '12 at 04:08
  • This is some code on asp.net forums dealing with the problem of the delete event firing twice – ONYX Jan 10 '12 at 04:11