Преглед на файлове

improve the frmBCRegulate

GSP преди 3 седмици
родител
ревизия
717cd69e50

+ 18 - 5
OTSIncAMeasureApp/5-OTSMeasureStatuImageFun/frmBCRegulate.Designer.cs

@@ -59,6 +59,7 @@
             this.radiobtnEveryPeriod = new System.Windows.Forms.RadioButton();
             this.txtWD = new System.Windows.Forms.TextBox();
             this.label8 = new System.Windows.Forms.Label();
+            this.label9 = new System.Windows.Forms.Label();
             this.groupBox2.SuspendLayout();
             this.SuspendLayout();
             // 
@@ -181,9 +182,9 @@
             // 
             // button2
             // 
-            this.button2.Location = new System.Drawing.Point(32, 265);
+            this.button2.Location = new System.Drawing.Point(69, 265);
             this.button2.Name = "button2";
-            this.button2.Size = new System.Drawing.Size(185, 42);
+            this.button2.Size = new System.Drawing.Size(124, 42);
             this.button2.TabIndex = 10;
             this.button2.Text = "AutoBrightContrast";
             this.button2.UseVisualStyleBackColor = true;
@@ -235,7 +236,7 @@
             // 
             // button4
             // 
-            this.button4.Location = new System.Drawing.Point(313, 79);
+            this.button4.Location = new System.Drawing.Point(313, 95);
             this.button4.Name = "button4";
             this.button4.Size = new System.Drawing.Size(60, 21);
             this.button4.TabIndex = 16;
@@ -275,9 +276,9 @@
             // 
             // button8
             // 
-            this.button8.Location = new System.Drawing.Point(234, 267);
+            this.button8.Location = new System.Drawing.Point(199, 266);
             this.button8.Name = "button8";
-            this.button8.Size = new System.Drawing.Size(92, 40);
+            this.button8.Size = new System.Drawing.Size(124, 42);
             this.button8.TabIndex = 20;
             this.button8.Text = "stop";
             this.button8.UseVisualStyleBackColor = true;
@@ -351,11 +352,22 @@
             this.label8.TabIndex = 23;
             this.label8.Text = "WD:";
             // 
+            // label9
+            // 
+            this.label9.AutoSize = true;
+            this.label9.ForeColor = System.Drawing.Color.Red;
+            this.label9.Location = new System.Drawing.Point(334, 280);
+            this.label9.Name = "label9";
+            this.label9.Size = new System.Drawing.Size(83, 12);
+            this.label9.TabIndex = 24;
+            this.label9.Text = "working state";
+            // 
             // frmBCRegulate
             // 
             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
             this.ClientSize = new System.Drawing.Size(494, 492);
+            this.Controls.Add(this.label9);
             this.Controls.Add(this.label8);
             this.Controls.Add(this.txtWD);
             this.Controls.Add(this.groupBox2);
@@ -426,5 +438,6 @@
         private System.Windows.Forms.Label label7;
         private System.Windows.Forms.TextBox txtWD;
         private System.Windows.Forms.Label label8;
+        private System.Windows.Forms.Label label9;
     }
 }

+ 57 - 4
OTSIncAMeasureApp/5-OTSMeasureStatuImageFun/frmBCRegulate.cs

@@ -74,6 +74,7 @@ namespace OTSMeasureApp._5_OTSMeasureStatuImageFun
             txtperiod.Text = period.ToString();
             txtWD.Text = workingDistance.ToString("F2");    
             EnableAutoBC.Checked = isToRun;
+            label9.Text = "";
         }
 
         private void button1_Click(object sender, EventArgs e)
@@ -102,13 +103,65 @@ namespace OTSMeasureApp._5_OTSMeasureStatuImageFun
             param.initialBrightness = double.Parse(txtinitbr.Text);
             param.initialContrast = double.Parse(txtinitcontrast.Text);
             param.toRun = EnableAutoBC.Checked;
+            param.workingDistance= double.Parse(txtWD.Text); 
             m_measureobj.m_pMeasureParam.GetDefaultParam().BrightnessContrastRegulationParam = param;
             BCregulater = new CBrightnessContrastAdjust(m_measureobj);
-         
-            Thread thread = new Thread(new ThreadStart(BCregulater.DoBrightnessContrastAdjust));
+
+            // disable the button while worker runs
+            button2.Enabled = false;
+            label9.Text = "Auto BC Adjusting...";
+            // start worker on a background thread and ensure callback runs on UI thread when done
+            Thread thread = new Thread(() =>
+            {
+                try
+                {
+                    BCregulater.DoBrightnessContrastAdjust();
+                }
+                catch (ThreadAbortException)
+                {
+                    // respect thread aborts if any; still call callback to restore UI
+                }
+                catch (Exception ex)
+                {
+                    // optionally log exception - keep UI responsive
+                    try {  } catch { }
+                }
+                finally
+                {
+                    OnBCAdjustFinished();
+                }
+            });
+            thread.IsBackground = true;
             thread.Start();
-          
-            
+        }
+
+        /// <summary>
+        /// Callback invoked when background BC adjust finishes (success, failure or stop).
+        /// Marshals back to UI thread and re-enables button2.
+        /// </summary>
+        private void OnBCAdjustFinished()
+        {
+            if (this.IsHandleCreated && this.InvokeRequired)
+            {
+                try
+                {
+                    this.BeginInvoke((Action)(() =>
+                    {
+                        try {
+                            button2.Enabled = true;
+                            label9.Text = "Auto BC Done!";
+                        } catch { }
+                    }));
+                }
+                catch { /* form might be closing */ }
+            }
+            else
+            {
+                try { 
+                    button2.Enabled = true;
+                    label9.Text = "Auto BC Done!";
+                } catch { }
+            }
         }
 
         private void button3_Click(object sender, EventArgs e)