AGoodDisplayName
AGoodDisplayName

Reputation: 5653

Simple Loading Form/Threading

In a legacy app we have a single winform that locks up while executing code on a button click...well it doesn't lock up it just "stops responding" until the code executes.

What is the best (least amount of code and working) way to wrap the code below into a separate thread and show a loading window (frmLoading) while it executes? I know this should be relatively simple, but I have tried a few different things that haven't quite worked out.

private void btnSynch_Click(object sender, EventArgs e)
{
    btnSynch.Enabled = false;

    if(chkDBSynch.Checked)
        PerformDBSyncronization();

    if(chkAppSynch.Checked)
        PerformApplicationSyncronization();

    btnSynch.Enabled = true;
}

EDIT: Ok, I should have mentioned have tried backgroundworker, but I figured out where I was tripping up....This code would execute and the loading form was getting thrown behind the main form which is why I thought it wasn't working. Can Anyone tell me how to prevent this from happening?

  private void btnSynch_Click(object sender, EventArgs e)
    {

        btnSynch.Enabled = false;
        frmLoading loadingWindow = new frmLoading();
        loadingWindow.Show();

        BackgroundWorker bw = new BackgroundWorker();
        bw.DoWork += (s, args) =>
        {

            Thread.Sleep(6000); //TODO:just for testing 
            if(chkDBSynch.Checked)
            PerformDBSyncronization();

            if(chkAppSynch.Checked)
            PerformApplicationSyncronization();
        };
        bw.RunWorkerCompleted += (s, args) =>
        {
            loadingWindow.Close();
        };
        bw.RunWorkerAsync();

        btnSynch.Enabled = true;
    }

Upvotes: 4

Views: 6470

Answers (2)

AGoodDisplayName
AGoodDisplayName

Reputation: 5653

I feel silly...The loading form was popping up behind the the main form which is why I thought it wasn't opening. I set the TopMost property to true and everything works as expected.

Edit:
Here is a good explanation why my form was popping up behind my window, I just wasn't expecting the thread that was opening the window to not use the main form as the owner.

Why use a owner window in MessageBox.Show?

I ended up with my code looking like

        frmLoading loadingWindow = new frmLoading();
        loadingWindow.Show(this);

        BackgroundWorker bw = new BackgroundWorker();
        bw.DoWork += (s, args) =>
        {
            this.Invoke(new MethodInvoker(() => this.Enabled = false));

            if(chkDBSynch.Checked)
            PerformDBSyncronization();

            if(chkAppSynch.Checked)
            PerformApplicationSyncronization();
        };
        bw.RunWorkerCompleted += (s, args) =>
        {
            loadingWindow.Close();
            this.Invoke(new MethodInvoker(() => this.Enabled = true));

        };

        bw.RunWorkerAsync();

Upvotes: 2

jonsca
jonsca

Reputation: 10381

Use a BackgroundWorker for this purpose.

Upvotes: 0

Related Questions