windowsforms-405538-diagnostics-and-troubleshooting-troubleshooting-performance.md
Identify performance bottlenecks and optimize application startup, control initialization/rendering, and data access.
When you build a .NET application, it is compiled to Microsoft Intermediate Language (MSIL). At runtime, the Just-In-Time (JIT) compiler translates MSIL into native machine code. This process may introduce startup delays.
Use Ahead-of-time (AOT) compilation to partially compile MSIL into native code at publish time. This technique reduces the need for JIT compilation and improves startup performance.
For example, you can publish your application in ReadyToRun (R2R) format to speed up startup. The application file size may increase because executables contain both native and MSIL code.
Activate the <PublishReadyToRun> option in the .csproj file:
<PropertyGroup>
<PublishReadyToRun>true</PublishReadyToRun>
</PropertyGroup>
Refer to the following help topic for more information: Reduce Application Launch Time.
At startup, the .NET runtime creates the form, initializes UI controls, applies layout logic, sets up event handlers, and applies skins. Each of these steps contributes to the overall startup time.
The more complex the UI, the longer these operations take. Multiple UI controls and nested hierarchies can slow down form initialization and rendering.
To improve startup performance, minimize the number of controls per form or user control and simplify layouts wherever possible.
Tip
You can delay form display until all controls are fully initialized. Override the main form’s XtraForm.ShowMode property to return FormShowMode.AfterInitialization:
protected override FormShowMode ShowMode {
get {
return FormShowMode.AfterInitialization;
}
}
Protected Overrides ReadOnly Property ShowMode() As FormShowMode
Get
Return FormShowMode.AfterInitialization
End Get
End Property
The following techniques reduce initialization time and preserve a rich user interface:
Use logical modules with fewer controls, each implemented as an XtraUserControl. Display one module at a time.
Use navigation containers to host groups of controls that do not need to be visible immediately. Controls within inactive tabs or frames are not fully initialized until the user activates the tab or frame.
Supported containers include:
If a view contains a DocumentManager with multiple documents, use Deferred Load. In this mode, inactive documents do not load their content when the application starts.
A performance drop may relate to events that customize individual elements within a control:
Review and optimize code in such event handlers.
Data access operations may affect performance due to query execution time, network latency, data volume, concurrent transactions, etc.
To find bottlenecks, use a database performance profiling tool (for example, SQL Server Profiler).
For more information, refer to the following help topic: Performance Optimization.
The Windows Forms platform or DevExpress controls may silently catch exceptions. A rapid performance drop may indicate a repeated unhandled exception.
Enable exception handling and identify/fix exceptions (if any). Refer to the following help topic for more information: Application Crashes or Throws an Exception.
When a WinForms application runs on a remote client (for example, Remote Desktop), the application constantly takes snapshots of its windows to render the UI on the client side. Visual effects, animations, or complex UIs can increase CPU and network load, which impacts performance.
To optimize performance in remote sessions, set the WindowsFormsSettings.OptimizeRemoteConnectionPerformance property to DefaultBoolean.True at application startup:
using DevExpress.XtraEditors;
using DevExpress.Utils;
namespace test {
internal static class Program {
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
WindowsFormsSettings.OptimizeRemoteConnectionPerformance = DefaultBoolean.True;
Application.Run(new MainView());
}
}
}
Imports DevExpress.XtraEditors
Imports DevExpress.Utils
Namespace test
Friend Module Program
<STAThread>
Sub Main()
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(False)
WindowsFormsSettings.OptimizeRemoteConnectionPerformance = DefaultBoolean.True
Application.Run(New Form1())
End Sub
End Module
End Namespace
Flickering occurs when a form executes long-running operations during initialization. In WinForms, a form calculates the layout, binds events, and instantiates and renders controls.
If these stages take a noticeable amount of time, the form may appear partially rendered and exhibit visual flicker.
To eliminate flickering, use an XtraForm (or its descendant) and display the form only after it is fully initialized. Override the XtraForm.ShowMode property to delay form display until all initialization steps are complete:
public partial class Form1 : DevExpress.XtraEditors.XtraForm {
public Form1() {
InitializeComponent();
}
protected override FormShowMode ShowMode {
get {
return FormShowMode.AfterInitialization;
}
}
}
Public Partial Class Form1
Inherits DevExpress.XtraEditors.XtraForm
Public Sub New()
InitializeComponent()
End Sub
Protected Overrides ReadOnly Property ShowMode As FormShowMode
Get
Return FormShowMode.AfterInitialization
End Get
End Property
End Class
When image data is stored as byte arrays, a data-aware control (for example, Data Grid, Tree List, Gantt Control, or Vertical Grid) converts each byte array to a raster or SVG image every time a cell is painted. Repeated conversions during scrolling or refresh operations can significantly reduce performance.
To optimize rendering, work with Image objects (do not store image data as byte arrays in your bound records). For example, create a Data Transfer Object (DTO) that stores an Image instance, and convert the byte array to an image only once on object initialization:
public class Employee {
public Employee(byte[] photoBytes) {
using(var ms = new MemoryStream(photoBytes))
Photo = Image.FromStream(ms);
}
public Image Photo { get; }
}
Public Class Employee
Public Sub New(ByVal photoBytes() As Byte)
Using ms = New MemoryStream(photoBytes)
Photo = Image.FromStream(ms)
End Using
End Sub
Public ReadOnly Property Photo() As Image
End Class
Tip
To detect resource-consuming conversion and other rendering-related issues, call the WindowsFormsSettings.ForcePaintApiDiagnostics method at application startup. A ByteArrayToImageConversionWarning exception is thrown for each repeated image conversion.
using DevExpress.XtraEditors;
using DevExpress.Utils.Diagnostics;
namespace test {
internal static class Program {
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
WindowsFormsSettings.ForcePaintApiDiagnostics(PaintApiDiagnosticsLevel.Throw);
Application.Run(new Form1());
}
}
}
Imports DevExpress.XtraEditors
Imports DevExpress.Utils.Diagnostics
Namespace test
Friend Module Program
<STAThread>
Sub Main()
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(False)
WindowsFormsSettings.ForcePaintApiDiagnostics(PaintApiDiagnosticsLevel.Throw)
Application.Run(New Form1())
End Sub
End Module
End Namespace
The Visual Studio Designer can experience performance degradation or freezes due to design-time exceptions, installed Visual Studio extensions, or the open Document Outline window.
To speed up the Visual Studio Designer, do the following:
Deactivate all Visual Studio extensionsEliminate extension-related issues.Close the Document Outline window Reduce layout recalculations and UI updates during design-time rendering.Check for design-time exceptions Obtain the exception’s call stack and investigate the stack trace to identify the culprit. Refer to the following help topic for more information: Application Crashes or Throws an Exception.
This troubleshooting guide mentions the most common performance-related issues. If none of the techniques listed here apply to your application, search the web or use the DevExpress.com Search Engine. If you cannot resolve your issue, submit a ticket to our Support Center. To help us assist you efficiently, please include:
See Also