How to show real-time updates in a grid using .NET

I have a good amount of experience in real-time trading systems. One area of expertise is in developing GUIs that can handle real-time updates. I want to explain and show you some of the techniques I use when developing a GUI to handle this many updates.

Task:

  • Create a GUI to receive real-time updates and display them in a grid.
  • Use a DataTable and bind it to a Grid.
  • Use a background Thread to simulate real-time updates.

Issues to be aware:

  • Updates can and will be occurring faster then you GUI can handle.
  • The GUI must always be responsive. Users do NOT like it when the application freezes because of updates occuring.
  • Updates will not be real-time. It will appear to be real-time because of the implementation, but users should not see any noticeable delays.

Practices to implement:

  • Cache the information and let the updates hit your cache. The cache will always keep the most recent information available. The GUI is not fast enough to keep up with the updates but your cache can.
  • The updates probably come from a background thread so make sure the cache is thread safe.
  • The cache should raise events when information is updated. This will signal a GUI of changes.
  • The GUI should have a queue of pending updates. Updates can be dequeued in batch, and not interrupt the GUI every time events fire.
  • Sometimes items will be updated more then once and will already exist in the queue. In this case we should not insert it into the queue again. Only one instance of the item should exist inside the queue.If we have 10,000 items and each item gets updates twice then only 10,000 items should be in the queue. We only show the most recent data in the grid.
  • A GUI Timer should be used to drain the queue and apply updates. The time should run every .5 seconds draining a max of 500 items that need to be updated.
  • When de-queuing items attempt to acquire a lock on and continue if unsuccessful. You dont want to lock the GUI thread waiting for lock because a background thread hasnt released a lock.
  • Use DataRow.BeginEdit/EndEdit because it will trigger only 1 update event per row. Otherwise, you trigger an update event for every column that gets changed.
  • Choose the right grid component because not all grids are designed to support real-time updates. The System.Windows.Forms.DataGrid is NOT the right component. This grid is not designed to handle many updates per second. I recommend using the Syncfusion grid component.

(Attached you can find an example application with the practices I described above.)

AttachmentSize
RealTimeGridUpdateTest.zip54.26 KB