Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am working on a windows application which freezes most of the time on button click events on Home Page. Please find the code below for your reference. Thanks


C#
using System;
using System.Windows.Forms;

namespace FileMigrationAgen
{
    public partial class HomePage : Form
    {
        public HomePage()
        {
            InitializeComponent();
        }

        private void tableLayoutPanel4_Paint(object sender, PaintEventArgs e)
        {

        }

        private async void button1_Click(object sender, EventArgs e)
        {
            SharepointMigration sharepointMigration = new SharepointMigration();
            sharepointMigration.Show();
            this.Hide();
            }

        private async void button2_Click(object sender, EventArgs e)
        {
            OneDriveMigration oneDriveMigration = new OneDriveMigration();
            oneDriveMigration.Show();
            this.Hide();            
        }


        private void HomePage_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }
    }
}


What I have tried:

Tried adding Form closed event in case threads were not getting disposed properly but didn't help.
Posted
Updated 24-Apr-20 2:56am
Comments
Richard Deeming 24-Apr-20 6:43am    
Why are the button-click event handler methods marked as async? Aside from the fact that you should avoid async void methods[^], there are no await statements in any of them, so the async modifier serves no purpose.

We have no idea what your forms are doing - but there is one UI thread which is shared across all forms (and controls) so if your new form constructors, or Load / Shown event handlers take considerable time then the main form will likely "freeze" until they are finished.

Start by looking at the constructors and "form startup event" handlers and see if they are doing anything that can be offloaded into a background thread: remember that the main form will not "Hide" until the constructor and Show method return.
 
Share this answer
 
Comments
Member 14734054 24-Apr-20 4:52am    
Yes your suggestion did help me in someway so I added shown() in all the forms. Now when I am navigating to other forms from homepage it is working fine.

But in between the forms there are buttons to go to other forms there it is still freezing. Please suggest what I can do for this.
OriginalGriff 24-Apr-20 5:01am    
We can't tell you "do this and it'll be fixed" because we have no access to your code or your app while it's running. You need to look at what your code does, and find out where it's "busy" - and we can't do that for you!
Member 14734054 25-Apr-20 9:32am    
Sure. Thanks for your reply.
More than likely the "work" being performed inside of constructor for the object SharepointMigration or the during the Show method is taking a lot of resources. The application freezes because the work is being done on the Main UI thread. You should separate the work into two parts - that which can be run on a separate thread (does not require the UI) and that which will require the UI. Try this. Change your button click events to a similar pattern as below:

First, create an extension method to help as explained here: C# - UIthread-Invoke Backgroundworker


private async void button1_Click(object sender, EventArgs e)
{
	//Make the constructor simple and not time consuming, 
	//only initialize variables with basic values
	//You can do the "work" in a seperate method that 
	//will be called  after the form is constructed.
	//To work correctly, the main UI thread needs 
	//to create/construct the form.
    SharepointMigration sharepointMigration = new SharepointMigration();
    
    //using async here because your button signature in 
    //your post used async. If the method does not need 
    //to be async then remove async and the await operator.  
    //The ContinueWith allows the form to show when the 
    //worker task - sharepointMigration.InitializeForm()
    //completes
    await Task.Run(async() => 
await sharepointMigration.InitializeForm()).ContinueWith((x) => {   

    	//Note the use of the extension method which 
    	//ensures the code executes on the Main UI Thread
    	sharepointMigration.UIThreadInvoke(() => {
    	
		    sharepointMigration.Show();
		    this.Hide();
	    });
    });
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900