I have the following stored procedure to retrieve a new datetime:
CREATE PROCEDURE [dbo].[GetTransactionTime]
AS
BEGIN
SELECT [TransactionTime] = GetUTCDate()
END
To call this SP i use a simple C# dataset on a local DB.
Since the datetime from SQL has a precision of 3.3ms i have to wait at least 3.3ms to get an new datetime. See more about the precision at msdn: http://msdn.microsoft.com/en-us/library/ms187819.aspx
In the example below i will wait 5ms:
class Program
{
static void Main(string[] args)
{
var adapter = new SPTableAdapters.TransactionTimeTableAdapter();
do
{
GetTransactionTime(
new Func<DateTime>(() => { return (DateTime)adapter.GetTransactionTimeNOW(); })
);
} while (true);
}
public static DateTime lastTransactionTime = DateTime.MinValue;
private static bool IsTransactionTimeUnique(DateTime newTransactionTime)
{
if (newTransactionTime > lastTransactionTime)
{
lastTransactionTime = newTransactionTime;
return true;
}
return false;
}
public static void GetTransactionTime(Func<DateTime> funcGetTransactionTime)
{
DateTime newTransactionTime = funcGetTr开发者_JAVA百科ansactionTime();
//MSSQL datetime has a off 3.33ms; try 5ms
DateTime now = DateTime.UtcNow;
DateTime max = now.AddMilliseconds(5);
while (!IsTransactionTimeUnique(newTransactionTime))
{
DateTime backup = DateTime.UtcNow;
if (backup > max) //if we tried more than 5ms.
{
//Always try once again.
newTransactionTime = funcGetTransactionTime();
if (IsTransactionTimeUnique(newTransactionTime))
{
break;
}
Console.WriteLine("FAILED: Old datetime: " + lastTransactionTime.ToBinary() + " New datetime: " + newTransactionTime.ToBinary() + "Start: " + now.Millisecond + " End: " + backup.Millisecond + " Max: " + max.Millisecond);
return; ;
}
newTransactionTime = funcGetTransactionTime();
}
Console.WriteLine("OK!");
}
}
The output:
FAILED: Old: 487 New: 487 Start: 479 End: 484 Max: 484
FAILED: Old: 487 New: 487 Start: 484 End: 489 Max: 489
FAILED: Old: 487 New: 487 Start: 489 End: 494 Max: 494
OK!
FAILED: Old: 503 New: 503 Start: 495 End: 500 Max: 500
FAILED: Old: 503 New: 503 Start: 500 End: 505 Max: 505
OK!
FAILED: Old: 517 New: 517 Start: 510 End: 515 Max: 515
FAILED: Old: 517 New: 517 Start: 515 End: 520 Max: 520
FAILED: Old: 517 New: 517 Start: 520 End: 525 Max: 525
OK!
FAILED: Old: 533 New: 533 Start: 526 End: 531 Max: 531
FAILED: Old: 533 New: 533 Start: 531 End: 536 Max: 536
OK!
FAILED: Old: 550 New: 550 Start: 541 End: 546 Max: 546
FAILED: Old: 550 New: 550 Start: 546 End: 551 Max: 551
FAILED: Old: 550 New: 550 Start: 551 End: 556 Max: 556
OK!
MSSQL wont give me a unique datetime every 5ms, changing the 5ms to 20ms works but i don't understand why the 5ms doesn't work. Hope some one can clarify this.
Thanks in advance.
Here's couple of articles on the subject, but basically on Windows time gets updated less often than after every 3.3ms.
http://www.grahamwideman.com/gw/tech/dataacq/wintiming.htm
http://discuss.fogcreek.com/joelonsoftware/default.asp?cmd=show&ixPost=85520
There's also a msdn article on the precision of GetTickCount:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms724408(v=vs.85).aspx
Windows only updates its current date/time on a ~50ms cycle.
精彩评论