I have a C# application that generates a Sequential GUID for each row that I insert into a table. I expect the inserted GUIDs to be sequential, but sometimes they break sequence (in chunks).
Example:
These GUIDs are shown in the order that they are inserted.
Why are these 'sequential' GUIDs being created with such a large break of sequence?
Code used to generate sequential GUIDs:
class NativeMethods
{
[DllImport("rpcrt4.dll", SetLastError = true)]
public static extern int UuidCreateSequential(out Guid guid);
}
public static Guid CreateSequentialGuid()
{
const int RPC_S_OK = 0;
Guid guid;
int result = NativeMethods.UuidCreateSequential(out guid);
if (result == RPC_S_OK)
return guid;
else
return Guid.NewGuid(); //<--In debugging, this statement never runs.
}
Code used in a loop to insert new GUIDs and information into table:
Guid mySequentialGUID = CreateSequentialGuid();
string[] row = { item1,
item2,
item3,
sourceText,
encoding.ToString(),
mySequentialGUID.ToString()
};
var listViewItem = new ListViewItem(row);
myListView.Items.Add(listViewItem);
Edit: Please see this question to understand Sequential GUIDs:
I think the problem is that you assume that "sequential" means "increment by one". Although your example shows that this happens sometimes there's no actual guarantee in the documentation that it will. We could argue the definition of "sequence" or that maybe this function is named poorly or even wrong but at the end of the day, according to the documentation it appears to be working 100% exactly as defined:
Creates a GUID that is greater than any GUID previously generated by this function
For people that have a problem with the general concept of a "sequential GUID" use the definition of "sequence" that's about "following a logical order" and also try and remember the Melissa virus. This was mildly document by this line:
For security reasons, UuidCreate was modified so that it no longer uses a machine's MAC address to generate UUIDs. UuidCreateSequential was introduced to allow creation of UUIDs using the MAC address of a machine's Ethernet card.
So the "sequence" is basing the GUID off of the MAC address.
Back to the OP's question, my best guess for why you have GUIDs that don't increment by one each time is the simple fact that you are probably not the only one calling this function. This is a core Windows function and there's bound to be other components, programs, services, etc. that use it.