简体   繁体   中英

form taking too long to load due to dataset

Actually my dataset source is mysql and this is my code

 private void frmNewInstallment_Load_1(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'cricflip_RoyalResidencyDataSet.booked_homes' table. You can move, or remove it, as needed.
            this.booked_homesTableAdapter.Fill(this.cricflip_RoyalResidencyDataSet.booked_homes);


        }

But still it's taking long to load that form i want to eliminate that delay. Is there any way to do this asynchronously.

Performing heavy operations in Form_Load event is not advisable, I would suggest using thread/task to do this operations.

private void frmNewInstallment_Load_1(object sender, EventArgs e)
{
     // Start a thread to load data asynchronously.
     Thread t = new Thread(LoadData);
     t.Start();
}  


private void LoadData()
{
     this.booked_homesTableAdapter.Fill(this.cricflip_RoyalResidencyDataSet.booked_homes);

    // Check if this code is executed on some other thread than UI thread
    if (InvokeRequired) // In this example, this will return `true`.
    {
        BeginInvoke(new Action(() =>
        {
            // Update your UI controls.
        }));
    }
}

Don't use the form load event. Make a background worker, during the form load event, start the background worker. Have the background worker update the GUI, so users know it's still busy.

Example; https://msdn.microsoft.com/en-us/library/cc221403(v=vs.95).aspx

You don't need different thread for this. During loading data from database thread do nothing - only waiting for response from database.

With async/await approach you will use same UI thread which will be freed during waiting for response.

Form.Load eventhandler seems good enough place for this

Mark Load eventhandler as async and make your database call async too.

public async Task<DataTable> GetData()
{
    //Your load logic
}

private async void Form_Load(object sender, EventArgs e)
{
    this.ComboBox.DataSource = await GetData();
}

Asynchronous Programming with async and await (C#)

async/await, as I understand, was designed exactly for IO processes.

You will need different threads only when your calculating/processing some data already in memory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM