Magic Of DataGridView

I have a windows form containing a text box and datagridview. As soon as the text of the text field changes, the datagridview data source is updated. This is done by handling the TextChanged event in the text field and assigning the data source, for example, a new list. Typically, a new data source contains 1000 rows, a fairly large amount of data. In addition, querying the database to retrieve this list also takes a lot of time.

What surprises me is that if I type fast enough in the text box, then the datagrid pauses the rendering again and again, and it only gives the final result. He completely ignores the average results. This makes sense because the working UI thread is busy during consecutive text changes. But why can I still enter a text box?

  • Textchange
  • Data retrieval
  • Updating DataGridView (no rendering / drawing)
  • Textchange
  • Data retrieval
  • Updating DataGridView (no rendering / drawing)
  • ... TextChange
  • Data retrieval
  • Updating DataGridView (final rendering / painting)
  • Done.

It looks like the text box was in a different thread, but it is absolutely not. Does anyone know why?

+6
source share
1 answer

This does not apply to DataGridView, any Windows window automatically has this ability. It is provided in the message queue, a data structure associated with the stream that the window displays. When a thread is busy with something else, Windows adds a message to the queue for an input event, such as a mouse click or keyboard key.

As soon as the thread finishes what it does, it again enters the message loop (the one that starts Application.Run ()), extracts the message from the queue, and processes it. Which rotation triggers the TextChange event. So a good way to visualize a queue is with a buffer. It can store up to 10,000 events by default.

+6
source

All Articles