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;
}