Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Below is a snippet of my codes for getting time ticks.

DateTime now = DateTime.Now;
// A double-click is detected if the the time elapsed
// since the last click is within DoubleClickTime.
string lines = "" + (now.Ticks - previousClick.Ticks);
System.IO.StreamWriter file = new System.IO.StreamWriter(@"e:\tmp0\test.txt", true);
file.WriteLine(lines);
file.Close();
if ((now.Ticks - previousClick.Ticks) <= 10000 * (SystemInformation.DoubleClickTime))
{
    // Raise the DoubleClick event.
    if (DoubleClick != null)
    {
        //MessageBox.Show("raise double click");
        DoubleClick(this, EventArgs.Empty);
    }
}
// Set previousClick to now so that
// subsequent double-clicks can be detected.
previousClick = now;


This section of codes are executed once for my each clicking. No matter how quick I click, I got very big tick difference between two clicks, as below. What's wrong with it? In my mind, I expect each difference should be just 1000 or so at most because its unit it milisecond.


635581521847201967
31031775
16800961
16720956
18001030
Posted
Updated 29-Jan-15 14:56pm
v2
Comments
Leo Chapiro 29-Jan-15 5:28am    
I cann't see where you set the previousClick.Ticks variable like: previousClick.Ticks = now.Ticks. Can you show this part of your code please?
BillWoodruff 29-Jan-15 8:24am    
The unit of a 'tick is not a millisecond:

"A single tick represents one hundred nanoseconds or one ten-millionth of a second. There are 10,000 ticks in a millisecond, or 10 million ticks in a second."
Stan Huang 29-Jan-15 20:57pm    
What BillWoodruff said is right.
_duDE_, I updated the codes which contains the statement you expect. The original codes is to simulate a double-click on picture box by single click.

I just tried it myself
Yes, I get same result: no matter how fast I type there is a limit.
This is probably due to the fact the OS has many processes and switches between tasks.
Otherwise we could not have multi-tasking on our computers.

static void Main(string[] args)
        {
            Console.WriteLine("DoubleClickTime = " + SystemInformation.DoubleClickTime.ToString());
            long ticks = 0;
            DateTime previous = DateTime.Now;
            char c = (char)0;
            while((c=Console.ReadKey().KeyChar)!='q')
            {
                DateTime now = DateTime.Now;
                ticks = now.Ticks - previous.Ticks;
                Console.WriteLine(": Ticks= {0}", ticks);
                previous = now;
            }
            Console.ReadKey();
        }
 
Share this answer
 
Comments
Stan Huang 30-Jan-15 0:20am    
As previously said by BillWoodruff, one second is 10 millions ticks. That's the reason why no matter how fast you typed, the number looks very big.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900