There is DateDiff() function in VB.NET but which function is equivalent to it in c#?

I m facing problem in converting this vb.net code to c#

Private LastPointWhenClicked As Point
     
    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    If LastPoint = Cursor.Position AndAlso LastPointWhenClicked <> Cursor.Position Then
    If DateDiff(DateInterval.Second, LastTimeMoved, Now) >= 2 Then
     
    LastPoint = New Point(Integer.MinValue, Integer.MinValue)
    LastPointWhenClicked = Cursor.Position
     
    End If
     
    Else
     
    LastPoint = Cursor.Position
    LastTimeMoved = Now
     
    End If
     
    End Sub

I would use TimeSpan.

using System;

namespace DW_401369
{
   class Program
   {
      static void Main(string[] args)
      {
         DateTime dtLastYear = new DateTime(2010, 12, 25);
         DateTime dtThisYear = new DateTime(2011, 12, 25);
         TimeSpan tsDiff = dtThisYear - dtLastYear;

         Console.WriteLine(tsDiff.Days);//365
      }
   }
}

But how to find time difference in seconds?

Here are both examples (using TimeSpan)

using System;

namespace DW_401369
{
   class Program
   {
      private static void ShowDaysInOneYear()
      {
         DateTime dtLastYear = new DateTime(2010, 12, 25);
         DateTime dtThisYear = new DateTime(2011, 12, 25);
         TimeSpan tsDiff = dtThisYear - dtLastYear;

         Console.WriteLine(tsDiff.Days);//365
      }

      private static void ShowSpinner(ushort numSeconds)
      {
         char[] arr_chrSpinner = new char[4] { '-', '\\', '|', '/' };

         DateTime dtNumSecondsFromNow = (DateTime.Now + new TimeSpan(0, 0, (int)numSeconds));

         uint uintLooper = 0;
         while (DateTime.Now < dtNumSecondsFromNow)
         {
            Console.Write("{0}{1}",
               arr_chrSpinner[uintLooper++ % arr_chrSpinner.Length], '\b');
         }
      }

      static void Main(string[] args)
      {
         ShowDaysInOneYear();
         ShowSpinner(2); // show a spinner on the console for two seconds
      }
   }
}

@thines
I m trying to build an application in c# where user moves mouse pointer at particular location and if the user doesnt move the mouse pointer from that location(i.e cursor coordinates does not change) for 2 seconds then action will be initiated.


help me with this how do i start? I m facing problem in timing!

...It's really the same thing -- counting for seconds to
1) Make something happen
2) Test to see if something happened

The concept is:
1) Set a bool to say the mouse has not been moved (set it to false)
2) Start the timer (while loop) checking for both time expiration AND mouse move
-if mouse has been moved, set the bool to true and "break"
-if mouse has not been moved, leave the bool set to false (let timer expire)
3) Take negative action if bool is still false

Hey @thines...
still need your help!

Here is my code...

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace mousy
{
    public partial class Form1 : Form
    {
        public Point pt;
        public Point lastpt;
        public DateTime t;
        

        public Form1()
        {
            
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            lastpt = Cursor.Position;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
           
                       
            DateTime dtNumSecondsFromNow = (DateTime.Now + new TimeSpan(0, 0, (int)(2)));
            if((lastpt == Cursor.Position) && pt!=Cursor.Position)
            {
                if (t < dtNumSecondsFromNow) 
                {
                    MessageBox.Show("Tada");
                    lastpt = new Point(Int16.MinValue, Int16.MinValue);
                    pt = Cursor.Position;

                }
               
            }
            else
                {

                    lastpt = Cursor.Position;
                    t = DateTime.Now;

                }
                                       

            
        }
    }
}

The problem with this code is that when I wait for a sec it keeps displaying the message box (action) until I again move my mouse!
I want the message box to be displayed only once if my cursor position doesnt change for 2 sec(or any specified seconds)

Please check! and suggest changes!

I have set timer1 interval to 200 milliseconds!

And Thankyou for helping me with this I m learning alot because of you!

If you put that call to DateTime.Now inside that timer function, it will continue to reset every time the timer runs and it will never expire.

line no. 40 keeps on executing if I stop the cursor movement! I want it to be execute only one time.

How are you starting and stopping the timer?
I also need to make sure I understand your design.
When your form opens, you're giving the user 2 seconds to move the mouse or message pops up, right?

If they move the mouse, is the problem solved or do they need to keep moving it in < 2-second increments?

...otherwise...
I scaled back the example and used this:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace DW_401369_WF
{
   public partial class Form1 : Form
   {
      public Point curPosPrevious;
      private DateTime dtNumSecondsFromNow = (DateTime.Now + new TimeSpan(0, 0, (int)(2)));

      public Form1()
      {
         InitializeComponent();
      }

      private void Form1_Load(object sender, EventArgs e)
      {
         curPosPrevious = Cursor.Position;
         timer1.Start();
      }

      private void timer1_Tick_1(object sender, EventArgs e)
      {
         if (curPosPrevious == Cursor.Position)
         {
            if (DateTime.Now >= dtNumSecondsFromNow)
            {
               timer1.Stop();
               MessageBox.Show("Tada");
            }
         }
         else
         {
            timer1.Stop();
         }
      }
   }
}

when the form opens, user can move cursor anywhere on screen and when the user stops cursor movement(new cursor coordinates) for 2 sec then
a message will pop up! then again user will move the cursor and when the user doesnt move the cursor for 2 sec again the same message will pop!
it is continued until form is closed!

OK.

using System;
using System.Drawing;
using System.Windows.Forms;

namespace DW_401369_WF
{
   public partial class Form1 : Form
   {
      public Point curPosPrevious = Cursor.Position;
      private DateTime dtNumSecondsFromNow = (DateTime.Now + new TimeSpan(0, 0, (int)(2)));

      public Form1()
      {
         InitializeComponent();
      }

      private void Form1_Load(object sender, EventArgs e)
      {
         Reset();
      }

      private void Reset()
      {
         dtNumSecondsFromNow = (DateTime.Now + new TimeSpan(0, 0, (int)(2)));
         timer1.Start();
         curPosPrevious = Cursor.Position;
      }

      private void timer1_Tick_1(object sender, EventArgs e)
      {
         if (curPosPrevious == Cursor.Position)
         {
            if (DateTime.Now >= dtNumSecondsFromNow)
            {
               timer1.Stop();
               MessageBox.Show("Tada");
               Reset();
            }
         }
         else
         {
            Reset();
         }
      }
   }
}

[In the Designer.cs]
You might also want to stop the timer when the form closes in an event handler like:

this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.MyFormClosing);

[...and also in the Form1.cs]

private void MyFormClosing(object sender, FormClosingEventArgs e)
{
   timer1.Stop();
}

Thankyou @thines

This works perfectly but if I m at same cursor position for 2 sec or more the message keeps popping up until i move my cursor!

private void timer1_Tick(object sender, EventArgs e)
        {
            if (curPosPrevious == Cursor.Position)
            {
                if (DateTime.Now >= dtNumSecondsFromNow)
                {
                    timer1.Stop();
                    System.Media.SystemSounds.Beep.Play();
                    Reset();
                }
            }
            else
            {
                Reset();
            }
        }

For example for this code if I stop moving my cursor it will play a beep sound repeatedly after every 2 sec.

I want to know how to play beep sound only once when cursor position does not change for 2 sec.

I got it! Thank you very much Tom!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.