Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The Magic Of DataGridView [closed]

I have a Windows form, containing a textbox and a datagridview. As soon as the text of textbox is changed, the datasource of the datagridview is updated. It is done by handling TextChanged event of the textbox and assigning the datasource a new List for example. Usually the new datasource contains 1000+ rows,fairly large amount of data. Also, querying the DB to fetch this list is also time consuming.

What surprises me is that if I type fast enough in the textbox, the datagrid suspend rendering itself again and again, and it only renders the final result. It completely ignores the middle results. This makes sense because the running UI thread is busy during consecutive text changes. But why can I still type in the textbox?

  1. TextChange
  2. Fetching Data
  3. Refreshing DataGridView (without rendering/painting)
  4. TextChange
  5. Fetching Data
  6. Refreshing DataGridView (without rendering/painting)
  7. ...TextChange
  8. Fetching Data
  9. Refreshing DataGridView (finaly rendering/painting)
  10. Done.

It is like that the textbox was on another thread but it is absolutely not. Does anybody know why?

like image 761
Alireza Avatar asked Dec 26 '12 19:12

Alireza


1 Answers

This is not specific to DataGridView, any Windows window automatically has this ability. It is provided by the message queue, a data structure that's associated with a thread that displays a window. When the thread is busy doing something else, Windows adds a message to the queue on an input event like a mouse button click or a keyboard key press.

Once the thread finishes what it is doing, it re-enters the message loop (the one started by Application.Run()), retrieves the message from the queue and handles it. Which turn causes the TextChange event to fire. So a good way to visualize the queue is as a buffer. It can store up to 10,000 events by default.

like image 51
Hans Passant Avatar answered Oct 04 '22 00:10

Hans Passant