Monday, November 19, 2007

 

DataGridView lockup in AdjustFillingColumns()

The other day, a WinForms application that I work on hung while resizing the form. Assuming that the lockup was somehow my/our fault, I naturally jumped in to investigate. As it turns out, the hang was not our fault at all.

The form was using a System.Windows.Forms.DataGridView control. I was able to attach my debugger to the process, and do a Break-All. The current thread was the UI thread, and the DataGridView's AdjustFillingColumns() method was at the top of the stack for that thread. After doing a run, wait a few seconds, and break-all several times, the current thread was always the UI thread and the AdjustFillingColumns() method was always on the top of the stack. Since task manager showed massive CPU usage while running and AdjustDataGridView() was always on top of the stack, it was a pretty good guess that the method was somehow in an infinite loop.

I used Reflector to look at the .NET code, and found 8 pages of nested if conditions and loops of every kinds. Luckily, I was able to use the Visual Studio Disassembler window to look at the assembly code and define the bounds of the loop in assembly code. Then, looking at the jumps and conditions of the assembly code, I was able to correlate it back to the Microsoft AdjustFillingColumns() source that was reverse-engineered by Reflector.

As it turns out, the AdjustFillingColumns() uses an iterative algorithm for determining the proper size of the auto-sized columns. Under certain conditions, the iterative algorithim does not converge and loops forever. The workaround is to not use any auto-sized columns, and instead calculate the column sizes in the application.

This issue has been reported to Microsoft. See the following Bug Report.

Upon re-reading this post, I realized that I forgot to specifically mention what I meant by "auto-sized columns". The problem I saw occurred when the DataGridViewColumn.AutoSizeMode property was set to DataGridViewAutoSizeColumnMode.Fill. I do not know if it occurs for other auto-size column modes.

Comments: Post a Comment





<< Home

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]