Take a few seconds to open Dialog Box in Windows Form Application

Phillip Stewart 0 Reputation points
2024-08-30T19:31:03.38+00:00

I have a C# project, which is a Windows Form application.

When clicking a button and opening dialog box, it takes a few seconds to open.

How can I improve this?

If needed, you can help me by having remote access to my PC.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,888 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,833 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,901 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jiale Xue - MSFT 44,926 Reputation points Microsoft Vendor
    2024-09-02T10:00:28.79+00:00

    Hi @Phillip Stewart , Welcome to Microsoft Q&A,

    The delay in opening the SaveGraphDialog may be due to the PopulateTreeView method, which is asynchronous and may take some time to complete, especially when the AuthAPI.GetUserEnterprises call involves network communication or other time-consuming operations.

    Show the Dialog First: You can show the dialog first and then populate the TreeView in the background. This approach will allow the dialog to appear immediately, and the tree will be populated afterward.

    public SaveGraphDialog()
    {
        InitializeComponent();
        folderInfos = new BindingList<FolderInfo>();
        parentFolderID = Guid.Empty;
        previouseParentFolderID = Guid.Empty;
        previousClientWidth = this.ClientSize.Width;
        previousClientHeight = this.ClientSize.Height;
        this.MinimumSize = new Size(900, 480);
    
        // Show the dialog and then populate the tree view
        this.Shown += async (sender, e) => await PopulateTreeViewAsync();
    }
    
    private async Task PopulateTreeViewAsync()
    {
        List<Enterprise> userEnterprises = await AuthAPI.GetUserEnterprises(AuthAPI._user_guid);
        foreach (Enterprise userEnterprise in userEnterprises)
        {
            TreeNode rootNode = new TreeNode(userEnterprise.Name);
            rootNode.Tag = userEnterprise;
            AddTenantsForEnterprise(rootNode, userEnterprise.GUID);
            treeView1.Nodes.Add(rootNode);
        }
    }
    

    If possible, preload the data before opening the dialog. This way, when the dialog opens, it can display the preloaded data immediately.

    Consider loading only the top-level nodes initially and then load child nodes as the user expands each node.

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.