From 79ce0564ac37ad1b15597526af8e47ab9f7343e5 Mon Sep 17 00:00:00 2001 From: Alessandro Magistroni <65421435+AMagistroni@users.noreply.github.com> Date: Mon, 30 Aug 2021 19:20:19 +0200 Subject: [PATCH] BackgroundWorker and progressbar --- .../MainForm.Designer.cs | 41 +++++++++---- SqlSchemaCompare.WindowsForm/MainForm.cs | 60 ++++++++++++------- SqlSchemaCompare.WindowsForm/MainForm.resx | 3 + 3 files changed, 71 insertions(+), 33 deletions(-) diff --git a/SqlSchemaCompare.WindowsForm/MainForm.Designer.cs b/SqlSchemaCompare.WindowsForm/MainForm.Designer.cs index 67c5104..abeeabf 100644 --- a/SqlSchemaCompare.WindowsForm/MainForm.Designer.cs +++ b/SqlSchemaCompare.WindowsForm/MainForm.Designer.cs @@ -70,6 +70,8 @@ private void InitializeComponent() this.ChkView = new System.Windows.Forms.CheckBox(); this.ChkTable = new System.Windows.Forms.CheckBox(); this.ChkAll = new System.Windows.Forms.CheckBox(); + this.BackgroundWorker = new System.ComponentModel.BackgroundWorker(); + this.ProgressBar = new System.Windows.Forms.ProgressBar(); this.groupBoxMain.SuspendLayout(); this.GrpUpdateSchema.SuspendLayout(); this.GrpCompare.SuspendLayout(); @@ -398,7 +400,7 @@ private void InitializeComponent() this.ChkOther.Size = new System.Drawing.Size(114, 24); this.ChkOther.TabIndex = 10; this.ChkOther.Text = "Other object"; - this.ChkOther.UseVisualStyleBackColor = true; + this.ChkOther.UseVisualStyleBackColor = true; // // ChkIndex // @@ -410,7 +412,7 @@ private void InitializeComponent() this.ChkIndex.Size = new System.Drawing.Size(67, 24); this.ChkIndex.TabIndex = 8; this.ChkIndex.Text = "Index"; - this.ChkIndex.UseVisualStyleBackColor = true; + this.ChkIndex.UseVisualStyleBackColor = true; // // ChkTrigger // @@ -422,7 +424,7 @@ private void InitializeComponent() this.ChkTrigger.Size = new System.Drawing.Size(78, 24); this.ChkTrigger.TabIndex = 7; this.ChkTrigger.Text = "Trigger"; - this.ChkTrigger.UseVisualStyleBackColor = true; + this.ChkTrigger.UseVisualStyleBackColor = true; // // ChkTableType // @@ -434,7 +436,7 @@ private void InitializeComponent() this.ChkTableType.Size = new System.Drawing.Size(100, 24); this.ChkTableType.TabIndex = 7; this.ChkTableType.Text = "Type table"; - this.ChkTableType.UseVisualStyleBackColor = true; + this.ChkTableType.UseVisualStyleBackColor = true; // // ChkSchema // @@ -446,7 +448,7 @@ private void InitializeComponent() this.ChkSchema.Size = new System.Drawing.Size(83, 24); this.ChkSchema.TabIndex = 6; this.ChkSchema.Text = "Schema"; - this.ChkSchema.UseVisualStyleBackColor = true; + this.ChkSchema.UseVisualStyleBackColor = true; // // ChkUser // @@ -458,7 +460,7 @@ private void InitializeComponent() this.ChkUser.Size = new System.Drawing.Size(60, 24); this.ChkUser.TabIndex = 5; this.ChkUser.Text = "User"; - this.ChkUser.UseVisualStyleBackColor = true; + this.ChkUser.UseVisualStyleBackColor = true; // // ChkFunction // @@ -470,7 +472,7 @@ private void InitializeComponent() this.ChkFunction.Size = new System.Drawing.Size(87, 24); this.ChkFunction.TabIndex = 4; this.ChkFunction.Text = "Function"; - this.ChkFunction.UseVisualStyleBackColor = true; + this.ChkFunction.UseVisualStyleBackColor = true; // // ChkStoreProcedure // @@ -482,7 +484,7 @@ private void InitializeComponent() this.ChkStoreProcedure.Size = new System.Drawing.Size(138, 24); this.ChkStoreProcedure.TabIndex = 3; this.ChkStoreProcedure.Text = "Store procedure"; - this.ChkStoreProcedure.UseVisualStyleBackColor = true; + this.ChkStoreProcedure.UseVisualStyleBackColor = true; // // ChkView // @@ -494,7 +496,7 @@ private void InitializeComponent() this.ChkView.Size = new System.Drawing.Size(63, 24); this.ChkView.TabIndex = 2; this.ChkView.Text = "View"; - this.ChkView.UseVisualStyleBackColor = true; + this.ChkView.UseVisualStyleBackColor = true; // // ChkTable // @@ -506,7 +508,7 @@ private void InitializeComponent() this.ChkTable.Size = new System.Drawing.Size(66, 24); this.ChkTable.TabIndex = 1; this.ChkTable.Text = "Table"; - this.ChkTable.UseVisualStyleBackColor = true; + this.ChkTable.UseVisualStyleBackColor = true; // // ChkAll // @@ -521,11 +523,26 @@ private void InitializeComponent() this.ChkAll.UseVisualStyleBackColor = true; this.ChkAll.CheckedChanged += new System.EventHandler(this.ChkAll_CheckedChanged); // + // BackgroundWorker + // + this.BackgroundWorker.DoWork += new System.ComponentModel.DoWorkEventHandler(this.LoadSchema); + this.BackgroundWorker.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.LoadSchemaCompleted); + // + // ProgressBar + // + this.ProgressBar.Location = new System.Drawing.Point(527, 628); + this.ProgressBar.Name = "ProgressBar"; + this.ProgressBar.Size = new System.Drawing.Size(176, 29); + this.ProgressBar.Style = System.Windows.Forms.ProgressBarStyle.Marquee; + this.ProgressBar.TabIndex = 15; + this.ProgressBar.Visible = false; + // // MainForm // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(724, 655); + this.ClientSize = new System.Drawing.Size(724, 669); + this.Controls.Add(this.ProgressBar); this.Controls.Add(this.GrpDbObjects); this.Controls.Add(this.GrpCompare); this.Controls.Add(this.GrpUpdateSchema); @@ -591,6 +608,8 @@ private void InitializeComponent() private System.Windows.Forms.CheckBox ChkTrigger; private System.Windows.Forms.CheckBox ChkTableType; private System.Windows.Forms.CheckBox ChkSchema; + private System.ComponentModel.BackgroundWorker BackgroundWorker; + private System.Windows.Forms.ProgressBar ProgressBar; } } diff --git a/SqlSchemaCompare.WindowsForm/MainForm.cs b/SqlSchemaCompare.WindowsForm/MainForm.cs index fd403aa..8987969 100644 --- a/SqlSchemaCompare.WindowsForm/MainForm.cs +++ b/SqlSchemaCompare.WindowsForm/MainForm.cs @@ -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); @@ -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) @@ -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) @@ -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(); @@ -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); } } @@ -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) @@ -364,5 +378,7 @@ private IEnumerable SelectedObjectType() return selectedObjectType; } - } + + public record ParametersLoad(string ErrorFile, string Origin, string Destination); + } } diff --git a/SqlSchemaCompare.WindowsForm/MainForm.resx b/SqlSchemaCompare.WindowsForm/MainForm.resx index 21bfd15..a39dcbe 100644 --- a/SqlSchemaCompare.WindowsForm/MainForm.resx +++ b/SqlSchemaCompare.WindowsForm/MainForm.resx @@ -69,4 +69,7 @@ 593, 17 + + 799, 17 + \ No newline at end of file