Time based collection and events
I would like to bulk insert a collection content every minute to a sql server using bulk insert.
I m using a ConcurrentBag to store requests in the memory.
What s the best practice to do this? or How can this be done?
I understand that I have to use events, should i wrap the collection within another class and 开发者_StackOverflowimplement expiration?
How do i use events for this purpose?
I think the requirement is not general purpose enough and the code to achieve this is small, so personally I think it's not necessary to create a separate class. At least if the containing class is not already overly long and complex. However this is more or less a matter of taste.
To achieve what you want, it should be enough to use the Timer
class and on every timer tick enumerate the collection inserting the items in the database and removing them from the collection at the same time. GetEnumerator on ConcurrentBag
works on a Snapshot of the data, so it's safe to remove the items while enumerating.
精彩评论