Skip to content

Commit

Permalink
BackgroundWorker and progressbar
Browse files Browse the repository at this point in the history
  • Loading branch information
AMagistroni committed Aug 30, 2021
1 parent 620f245 commit 79ce056
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 33 deletions.
41 changes: 30 additions & 11 deletions SqlSchemaCompare.WindowsForm/MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 38 additions & 22 deletions SqlSchemaCompare.WindowsForm/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ private void BtnCompare_Click(object sender, EventArgs e)
return;
}

DisableMainForm(PleaseWait);
EnableDisableMainForm(PleaseWait, false);

string fileNameDiffOrigin = GetFileNameDiff(txtOriginSchema.Text);
string fileNameDiffDestination = GetFileNameDiff(txtDestinationSchema.Text);
Expand All @@ -151,12 +151,12 @@ private void BtnCompare_Click(object sender, EventArgs e)
File.WriteAllText(fileNameDiffOrigin, file1);
File.WriteAllText(fileNameDiffDestination, file2);

EnableMainForm(FileCompareCreated);
EnableDisableMainForm(FileCompareCreated, true);
}
catch (Exception exc)
{
MessageBox.Show(exc.Message + exc.StackTrace);
EnableMainForm("");
EnableDisableMainForm(string.Empty, true);
}
}
private string GetFileNameDiff(string fileName)
Expand All @@ -168,16 +168,18 @@ private string GetFileNameDiff(string fileName)
return $"{fileName.Substring(0, indexDot)}{formSettings.Suffix}{fileName.Substring(indexDot)}";
}

private void DisableMainForm(string text)
private void EnableDisableMainForm(string text, bool enable)
{
lblInfo.Text = text;
this.Enabled = false;
}
groupBoxMain.Enabled = enable;
GrpCompare.Enabled = enable;
GrpDbObjects.Enabled = enable;
GrpUpdateSchema.Enabled = enable;

private void EnableMainForm(string text)
{
lblInfo.Text = text;
this.Enabled = true;
if (enable)
ProgressBar.Hide();
else
ProgressBar.Show();
}

private void LoadClearSchemaCompleted(string text, bool isAfterLoad)
Expand Down Expand Up @@ -246,7 +248,7 @@ private void BtnCreateUpdateFile_Click(object sender, EventArgs e)
if (File.Exists(errorFile))
File.Delete(errorFile);

DisableMainForm(PleaseWait);
EnableDisableMainForm(PleaseWait, false);

var (origin, destination) = GetSchema();

Expand All @@ -261,12 +263,12 @@ private void BtnCreateUpdateFile_Click(object sender, EventArgs e)

File.WriteAllText(txtUpdateSchemaFile.Text, stringResult.ToString());

EnableMainForm(FileUpdateCreated);
EnableDisableMainForm(FileUpdateCreated, true);
}
catch (Exception exc)
{
MessageBox.Show(exc.Message + exc.StackTrace);
EnableMainForm("");
EnableDisableMainForm(string.Empty, true);
}
}

Expand All @@ -285,27 +287,39 @@ private void BtnSwapOriginDestination_Click(object sender, EventArgs e)
txtOriginSchema.Text = txtDestinationSchema.Text;
txtDestinationSchema.Text = swap;
}

private void BtnLoadSchema_Click(object sender, EventArgs e)
{
if (!MandatoryFieldArePresent())
return;

var errorFile = GetErrorFileName();
if (File.Exists(errorFile))
File.Delete(errorFile);

DisableMainForm(PleaseWait);
EnableDisableMainForm(PleaseWait, false);
var (origin, destination) = GetSchema();


ParametersLoad parameters = new(errorFile, origin, destination);
BackgroundWorker.RunWorkerAsync(parameters);
}

private void LoadSchema(object sender, System.ComponentModel.DoWorkEventArgs e)
{
ParametersLoad parameters = e.Argument as ParametersLoad;
IDbObjectFactory dbObjectFactory = new TSqlObjectFactory();
var loadSchemaManager = new LoadSchemaManager(dbObjectFactory, errorWriter);
string errors;
(currentOriginDbObjects, currentDestinationDbObjects, errors) = loadSchemaManager.LoadSchema(origin, destination);

File.WriteAllText(errorFile, errors);
string errors;
(currentOriginDbObjects, currentDestinationDbObjects, errors) = loadSchemaManager.LoadSchema(parameters.Origin, parameters.Destination);
File.WriteAllText(parameters.ErrorFile, errors);
e.Result = errors;
}

LoadClearSchemaCompleted(string.IsNullOrEmpty(errors) ? SchemaLoaded : SchemaLoadedWithErrors, true);
private void LoadSchemaCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
ProgressBar.Hide();
LoadClearSchemaCompleted(string.IsNullOrEmpty(e.Result as string) ? SchemaLoaded : SchemaLoadedWithErrors, true);
}

private void BtnClear_Click(object sender, EventArgs e)
Expand Down Expand Up @@ -364,5 +378,7 @@ private IEnumerable<DbObjectType> SelectedObjectType()

return selectedObjectType;
}
}

public record ParametersLoad(string ErrorFile, string Origin, string Destination);
}
}
3 changes: 3 additions & 0 deletions SqlSchemaCompare.WindowsForm/MainForm.resx
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,7 @@
<metadata name="ofdUpdateSchemaFile.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>593, 17</value>
</metadata>
<metadata name="BackgroundWorker.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>799, 17</value>
</metadata>
</root>

0 comments on commit 79ce056

Please sign in to comment.