Skip to content

How to avoid infinite loops (application window will not appear) after upgrading to .Net to 4.7.x

Jan Karger edited this page May 2, 2019 · 2 revisions

For the benefit of people working with MahApps.Metro with similar problems who get to this issue, here's a summary and workaround when your application window does not appear after the .Net Framework upgrade.

Problem

After a basic upgrade of the .NET framework from 4.6.2. to 4.7.2 the application main window was never shown and was hang in an infinite loop. The mainwindow.show() function never returned back to the application. The issue is not MahApps related but as it is not easy to debug and spot, I might post the solution here if somehone runs into a similar situation using MahApps framework.

Root cause

.Net 4.7 included a new algorithm for allocating space to columns in a Grid declared with '*' in their widths. When an app is compiled for 4.7.x there's a possibility that grid sizing can cause the application to seize up in an infinite loop that can lock and freeze the application.

Originally posted by @SamBent in https://github.com/Microsoft/dotnet/issues/604#issuecomment-463341296

Workaround

Set the StarDefinitionsCanExceedAvailableSpace switch to true in the app.config to force using the old algorithm:

<configuration>
    <runtime>
        <AppContextSwitchOverrides value="Switch.System.Windows.Controls.Grid.StarDefinitionsCanExceedAvailableSpace=true" />
    </runtime>
</configuration>

Additional context

https://docs.microsoft.com/en-us/dotnet/framework/migration-guide/runtime/4.7-4.7.1#resizing-a-grid-can-hang

The reported problem above is fixed with this workaround. Hoping this might help others as the debug process can be quite hard. Again the bug is not MahApps related. This issue is a .net 4.7.x bug and not solved by Microsoft.

@whentotrade