InvokeRequired-优化UI控件跨线程处理

This commit is contained in:
2026-05-08 17:42:13 +08:00
parent c287760071
commit 8ef83ed848

View File

@@ -136,10 +136,14 @@ namespace 精工涂胶检测项目
String ProductSN;
private string ttt;
public static string CreateCurrentDateFolder(string rootDirectory)
{ return CreateCurrentDateFolder(rootDirectory, DateTime.Now); }
{
return CreateCurrentDateFolder(rootDirectory, DateTime.Now);
}
public static string CreateCurrentDateFolder(string rootDirectory,DateTime targetDate)
{ if (string.IsNullOrWhiteSpace(rootDirectory))
{
if (string.IsNullOrWhiteSpace(rootDirectory))
throw new ArgumentNullException(nameof(rootDirectory), "根目录不能为空");
string datrFolderName=targetDate.ToString("yyyyMMdd");
string fullFolderPath=Path.Combine(rootDirectory, datrFolderName);
Directory.CreateDirectory(fullFolderPath);
@@ -374,8 +378,16 @@ namespace 精工涂胶检测项目
AddLog("给PLC回复收到触发", 0);
cog3DDisplayV2WF1.Clear();
label34.Invoke(new Action(() => label34.Text = "正常生产中!!!!"));
//≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡【BEGIN】
//label34.Invoke(new Action(() => label34.Text = "正常生产中!!!!"));
//label34.BackColor = Color.Green; //改颜色也要跨线程处理
label34.Invoke(new Action(() =>
{
label34.Text = "正常生产中!!!!";
label34.BackColor = Color.Green;
}));
//≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡【END】
if (myJobManager.JobsRunningState == CogJobsRunningStateConstants.None)
{
myJobManager.Run();
@@ -773,8 +785,15 @@ namespace 精工涂胶检测项目
label2.Invoke(new Action(() => label2.Text = s));
Image img = cogRecordDisplay1.CreateContentBitmap(Cognex.VisionPro.Display.CogDisplayContentBitmapConstants.Display);
string imageName = s + DateTime.Now.ToString("ss") + ".Png";
imageSavePath = Path.Combine(currentDateFolder, imageName);
//≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡【BEGIN】
//string imageName = s + DateTime.Now.ToString("ss") + ".Png";
//imageSavePath = Path.Combine(currentDateFolder, imageName);
StringBuilder sb = new StringBuilder();
sb.Append(s + DateTime.Now.ToString("ss") + ".Png");
imageSavePath = Path.Combine(currentDateFolder, sb.ToString());
//≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡【BEGIN】
try
{
img.Save(imageSavePath, ImageFormat.Png);
@@ -1060,15 +1079,246 @@ namespace 精工涂胶检测项目
areaStatus3 = "NG";
}
//≡≡≡≡≡≡≡≡≡≡ ↓↓↓↓↓↓↓↓↓↓ ≡≡≡≡≡≡≡≡≡≡ ↓↓↓↓↓↓↓↓↓↓ ≡≡≡≡≡≡≡≡≡≡ ↓↓↓↓↓↓↓↓↓↓ ≡≡≡≡≡≡≡≡≡≡【BEGIN】【优化Invoke】
#region XXXX XXXX
//if (dw > 1)
//{
// if (label34.InvokeRequired)
// {
// label34.Invoke(new Action(() => label34.Text = "断胶了,请注意检查!!!!"));
// }
// resultStatus = "NG";
// modbusResult = 2;
//}
//if (volumStatus1 == "OK" && areaStatus1 == "OK" && volumStatus2 == "OK" && areaStatus2 == "OK" && volumStatus3 == "OK" && areaStatus3 == "OK" && dj < 2 && qx < 2)
//{
// resultStatus = "OK";
// modbusResult = 1;
// okNum++;
//}
//else if (dj > 1)
//{
// if (label34.InvokeRequired)
// {
// label34.Invoke(new Action(() => label34.Text = "断胶了,请注意检查!!!!"));
// label34.BackColor = Color.Red;
// }
// resultStatus = "NG";
// modbusResult = 2;
//}
//else if (qx > 1)
//{
// if (label34.InvokeRequired)
// {
// label34.Invoke(new Action(() => label34.Text = "断胶了,请注意检查!!!!"));
// label34.BackColor = Color.Red;
// }
// resultStatus = "NG";
// modbusResult = 2;
//}
//else
//{
// resultStatus = "NG";
// modbusResult = 2; //先观察数据,别忘记改2ng
// ngNum++;
//}
//CogImage16Range mImage = cogRecordDisplay1.Record.Content as CogImage16Range;
//string result = $"{volum1:F2},{area1:F2},{resultStatus}";
//;
//if (lbl_Volume1.InvokeRequired)
//{
// lbl_Volume1.Invoke(new Action(() => lbl_Volume1.Text = volum1.ToString("0.00" + "mm³")));
//}
//if (lbl_Area1.InvokeRequired)
//{
// lbl_Area1.Invoke(new Action(() => lbl_Area1.Text = area1.ToString("0.00" + "mm²")));
//}
//if (lbl_Volume2.InvokeRequired)
//{
// lbl_Volume2.Invoke(new Action(() => lbl_Volume2.Text = volum2.ToString("0.00" + "mm³")));
//}
//if (lbl_Area2.InvokeRequired)
//{
// lbl_Area2.Invoke(new Action(() => lbl_Area2.Text = area2.ToString("0.00" + "mm²")));
//}
//if (lbl_Volume3.InvokeRequired)
//{
// lbl_Volume3.Invoke(new Action(() => lbl_Volume3.Text = volum3.ToString("0.00" + "mm³")));
//}
//if (lbl_Area3.InvokeRequired)
//{
// lbl_Area3.Invoke(new Action(() => lbl_Area3.Text = area3.ToString("0.00" + "mm²")));
//}
//if (label54.InvokeRequired && myToolBlock2.RunStatus.Result == CogToolResultConstants.Accept)
//{
// label54.Invoke(new Action(() => label54.Text = bdktj.ToString("0.00" + "mm³")));
// if (bdktj > 74770 && bdktj < 75008)
// {
// label80.Invoke(new Action(() => label80.Text = ("OK")));
// label80.Invoke(new Action(() => label80.BackColor = Color.Green));
// }
// else
// {
// label80.Invoke(new Action(() => label80.Text = ("NG")));
// label80.Invoke(new Action(() => label80.BackColor = Color.Red));
// }
//}
//else
//{
// label54.Invoke(new Action(() => label54.Text = "0.00" + "mm²"));
// label80.Invoke(new Action(() => label80.Text = ("NG")));
// label80.Invoke(new Action(() => label80.BackColor = Color.Red));
//}
//if (volumStatus1 == "OK")
//{
// if (lbl_Volume1Status.InvokeRequired)
// {
// lbl_Volume1Status.Invoke(new Action(() => lbl_Volume1Status.Text = "合格"));
// lbl_Volume1Status.BackColor = Color.Green;
// }
//}
//else
//{
// if (lbl_Volume1Status.InvokeRequired)
// {
// lbl_Volume1Status.Invoke(new Action(() => lbl_Volume1Status.Text = "不合格"));
// lbl_Volume1Status.BackColor = Color.Red;
// label34.BackColor = Color.Red;
// if (label34.InvokeRequired)
// {
// label34.Invoke(new Action(() => label34.Text = "位置1体积检测超限!!!!"));
// }
// }
//}
//if (volumStatus2 == "OK")
//{
// if (lbl_Volume2Status.InvokeRequired)
// {
// lbl_Volume2Status.Invoke(new Action(() => lbl_Volume2Status.Text = "合格"));
// lbl_Volume2Status.BackColor = Color.Green;
// }
//}
//else
//{
// if (lbl_Volume2Status.InvokeRequired)
// {
// lbl_Volume2Status.Invoke(new Action(() => lbl_Volume2Status.Text = "不合格"));
// lbl_Volume2Status.BackColor = Color.Red;
// label34.BackColor = Color.Red;
// }
// if (label34.InvokeRequired)
// {
// label34.Invoke(new Action(() => label34.Text = "位置2体积检测超限!!!!"));
// }
//}
//if (volumStatus3 == "OK")
//{
// if (lbl_Volume3Status.InvokeRequired)
// {
// lbl_Volume3Status.Invoke(new Action(() => lbl_Volume3Status.Text = "合格"));
// lbl_Volume3Status.BackColor = Color.Green;
// GC.Collect();
// }
//}
//else
//{
// if (lbl_Volume3Status.InvokeRequired)
// {
// lbl_Volume3Status.Invoke(new Action(() => lbl_Volume3Status.Text = "不合格"));
// lbl_Volume3Status.BackColor = Color.Red;
// label34.BackColor = Color.Red;
// }
// if (label34.InvokeRequired)
// {
// label34.Invoke(new Action(() => label34.Text = "位置3体积检测超限!!!!"));
// }
//}
//if (areaStatus1 == "OK")
//{
// if (lbl_Area1Status.InvokeRequired)
// {
// lbl_Area1Status.Invoke(new Action(() => lbl_Area1Status.Text = "合格"));
// lbl_Area1Status.BackColor = Color.Green;
// }
//}
//else
//{
// if (lbl_Area1Status.InvokeRequired)
// {
// lbl_Area1Status.Invoke(new Action(() => lbl_Area1Status.Text = "不合格"));
// lbl_Area1Status.BackColor = Color.Red;
// label34.BackColor = Color.Red;
// }
// if (label34.InvokeRequired)
// {
// label34.Invoke(new Action(() => label34.Text = "位置1面积检测超限!!!!"));
// }
//}
//if (areaStatus2 == "OK")
//{
// if (lbl_Area2Status.InvokeRequired)
// {
// lbl_Area2Status.Invoke(new Action(() => lbl_Area2Status.Text = "合格"));
// lbl_Area2Status.BackColor = Color.Green;
// }
//}
//else
//{
// if (lbl_Area2Status.InvokeRequired)
// {
// lbl_Area2Status.Invoke(new Action(() => lbl_Area2Status.Text = "不合格"));
// lbl_Area2Status.BackColor = Color.Red;
// label34.BackColor = Color.Red;
// }
// if (label34.InvokeRequired)
// {
// label34.Invoke(new Action(() => label34.Text = "位置2面积检测超限!!!!"));
// }
//}
//if (areaStatus3 == "OK")
//{
// if (lbl_Area3Status.InvokeRequired)
// {
// lbl_Area3Status.Invoke(new Action(() => lbl_Area3Status.Text = "合格"));
// lbl_Area3Status.BackColor = Color.Green;
// }
//}
//else
//{
// if (lbl_Area3Status.InvokeRequired)
// {
// lbl_Area3Status.Invoke(new Action(() => lbl_Area3Status.Text = "不合格"));
// lbl_Area3Status.BackColor = Color.Red;
// label34.BackColor = Color.Red;
// }
// if (label34.InvokeRequired)
// {
// label34.Invoke(new Action(() => label34.Text = "位置3面积检测超限!!!!"));
// }
//}
//if (lbl_CheckNum.InvokeRequired)
//{
// lbl_CheckNum.Invoke(new Action(() => lbl_CheckNum.Text = (okNum + ngNum).ToString()));
// lbl_CheckNum.Invoke(new Action(() => label11.Text = okNum.ToString()));
// lbl_CheckNum.Invoke(new Action(() => label14.Text = ngNum.ToString()));
// double zs = Double.Parse((okNum + ngNum).ToString());
// double OKs = Double.Parse(okNum.ToString());
// lbl_CheckNum.Invoke(new Action(() => lbl_OkRate.Text = (OKs / zs).ToString("0.00%")));
//}
#endregion
if (dw > 1)
{
if (label34.InvokeRequired)
{
label34.Invoke(new Action(() => label34.Text = "断胶了,请注意检查!!!!"));
}
resultStatus = "NG";
modbusResult = 2;
}
if (volumStatus1 == "OK" && areaStatus1 == "OK" && volumStatus2 == "OK" && areaStatus2 == "OK" && volumStatus3 == "OK" && areaStatus3 == "OK" && dj < 2 && qx < 2)
{
resultStatus = "OK";
@@ -1077,25 +1327,13 @@ namespace 精工涂胶检测项目
}
else if (dj > 1)
{
if (label34.InvokeRequired)
{
label34.Invoke(new Action(() => label34.Text = "断胶了,请注意检查!!!!"));
label34.BackColor = Color.Red;
}
resultStatus = "NG";
modbusResult = 2;
}
else if (qx > 1)
{
if (label34.InvokeRequired)
{
label34.Invoke(new Action(() => label34.Text = "断胶了,请注意检查!!!!"));
label34.BackColor = Color.Red;
}
resultStatus = "NG";
modbusResult = 2;
}
else
{
@@ -1103,193 +1341,170 @@ namespace 精工涂胶检测项目
modbusResult = 2; //先观察数据,别忘记改2ng
ngNum++;
}
CogImage16Range mImage = cogRecordDisplay1.Record.Content as CogImage16Range;
string result = $"{volum1:F2},{area1:F2},{resultStatus}";
;
string result = $"{volum1:F2},{area1:F2},{resultStatus}"; //此变量貌似后面没用到
if (lbl_Volume1.InvokeRequired)
bool resultAccept = myToolBlock2.RunStatus.Result == CogToolResultConstants.Accept;
this.Invoke(new Action(() =>
{
lbl_Volume1.Invoke(new Action(() => lbl_Volume1.Text = volum1.ToString("0.00" + "mm³")));
}
if (lbl_Area1.InvokeRequired)
if (dw > 1)
{
lbl_Area1.Invoke(new Action(() => lbl_Area1.Text = area1.ToString("0.00" + "mm²")));
}
if (lbl_Volume2.InvokeRequired)
{
lbl_Volume2.Invoke(new Action(() => lbl_Volume2.Text = volum2.ToString("0.00" + "mm³")));
}
if (lbl_Area2.InvokeRequired)
{
lbl_Area2.Invoke(new Action(() => lbl_Area2.Text = area2.ToString("0.00" + "mm²")));
}
if (lbl_Volume3.InvokeRequired)
{
lbl_Volume3.Invoke(new Action(() => lbl_Volume3.Text = volum3.ToString("0.00" + "mm³")));
}
if (lbl_Area3.InvokeRequired)
{
lbl_Area3.Invoke(new Action(() => lbl_Area3.Text = area3.ToString("0.00" + "mm²")));
label34.Text = "断胶了,请注意检查!!!!";
//resultStatus = "NG";
//modbusResult = 2;
}
if (label54.InvokeRequired && myToolBlock2.RunStatus.Result == CogToolResultConstants.Accept)
//if (volumStatus1 == "OK" && areaStatus1 == "OK" && volumStatus2 == "OK" && areaStatus2 == "OK" && volumStatus3 == "OK" && areaStatus3 == "OK" && dj < 2 && qx < 2)
//{
// resultStatus = "OK";
// modbusResult = 1;
// okNum++;
//}
//else if (dj > 1)
if (dj > 1)
{
label54.Invoke(new Action(() => label54.Text = bdktj.ToString("0.00" + "mm³")));
label34.Text = "断胶了,请注意检查!!!!";
label34.BackColor = Color.Red;
//resultStatus = "NG";
//modbusResult = 2;
}
else if (qx > 1)
{
label34.Text = "断胶了,请注意检查!!!!";
label34.BackColor = Color.Red;
//resultStatus = "NG";
//modbusResult = 2;
}
//else
//{
// resultStatus = "NG";
// modbusResult = 2; //先观察数据,别忘记改2ng
// ngNum++;
//}
//CogImage16Range mImage = cogRecordDisplay1.Record.Content as CogImage16Range;
//string result = $"{volum1:F2},{area1:F2},{resultStatus}"; //此变量貌似后面没用到
lbl_Volume1.Text = volum1.ToString("0.00" + "mm³");
lbl_Area1.Text = area1.ToString("0.00" + "mm²");
lbl_Volume2.Text = volum2.ToString("0.00" + "mm³");
lbl_Area2.Text = area2.ToString("0.00" + "mm²");
lbl_Volume3.Text = volum3.ToString("0.00" + "mm³");
lbl_Area3.Text = area3.ToString("0.00" + "mm²");
//if (myToolBlock2.RunStatus.Result == CogToolResultConstants.Accept)
if (resultAccept)
{
label54.Text = bdktj.ToString("0.00" + "mm³");
if (bdktj > 74770 && bdktj < 75008)
{
label80.Invoke(new Action(() => label80.Text = ("OK")));
label80.Invoke(new Action(() => label80.BackColor = Color.Green));
label80.Text = "OK";
label80.BackColor = Color.Green;
}
else
{
label80.Invoke(new Action(() => label80.Text = ("NG")));
label80.Invoke(new Action(() => label80.BackColor = Color.Red));
label80.Text = "NG";
label80.BackColor = Color.Red;
}
}
else
{
label54.Invoke(new Action(() => label54.Text = "0.00" + "mm²"));
label80.Invoke(new Action(() => label80.Text = ("NG")));
label80.Invoke(new Action(() => label80.BackColor = Color.Red));
label54.Text = "0.00" + "mm²";
label80.Text = "NG";
label80.BackColor = Color.Red;
}
if (volumStatus1 == "OK")
{
if (lbl_Volume1Status.InvokeRequired)
{
lbl_Volume1Status.Invoke(new Action(() => lbl_Volume1Status.Text = "合格"));
lbl_Volume1Status.Text = "合格";
lbl_Volume1Status.BackColor = Color.Green;
}
}
else
{
if (lbl_Volume1Status.InvokeRequired)
{
lbl_Volume1Status.Invoke(new Action(() => lbl_Volume1Status.Text = "不合格"));
lbl_Volume1Status.Text = "不合格";
lbl_Volume1Status.BackColor = Color.Red;
label34.BackColor = Color.Red;
if (label34.InvokeRequired)
{
label34.Invoke(new Action(() => label34.Text = "位置1体积检测超限!!!!"));
}
}
label34.Text = "位置1体积检测超限!!!!";
}
if (volumStatus2 == "OK")
{
if (lbl_Volume2Status.InvokeRequired)
{
lbl_Volume2Status.Invoke(new Action(() => lbl_Volume2Status.Text = "合格"));
lbl_Volume2Status.Text = "合格";
lbl_Volume2Status.BackColor = Color.Green;
}
}
else
{
if (lbl_Volume2Status.InvokeRequired)
{
lbl_Volume2Status.Invoke(new Action(() => lbl_Volume2Status.Text = "不合格"));
lbl_Volume2Status.Text = "不合格";
lbl_Volume2Status.BackColor = Color.Red;
label34.BackColor = Color.Red;
label34.Text = "位置2体积检测超限!!!!";
}
if (label34.InvokeRequired)
{
label34.Invoke(new Action(() => label34.Text = "位置2体积检测超限!!!!"));
}
}
if (volumStatus3 == "OK")
{
if (lbl_Volume3Status.InvokeRequired)
{
lbl_Volume3Status.Invoke(new Action(() => lbl_Volume3Status.Text = "合格"));
lbl_Volume3Status.Text = "合格";
lbl_Volume3Status.BackColor = Color.Green;
GC.Collect();
}
}
else
{
if (lbl_Volume3Status.InvokeRequired)
{
lbl_Volume3Status.Invoke(new Action(() => lbl_Volume3Status.Text = "不合格"));
lbl_Volume3Status.Text = "不合格";
lbl_Volume3Status.BackColor = Color.Red;
label34.BackColor = Color.Red;
label34.Text = "位置3体积检测超限!!!!";
}
if (label34.InvokeRequired)
{
label34.Invoke(new Action(() => label34.Text = "位置3体积检测超限!!!!"));
}
}
if (areaStatus1 == "OK")
{
if (lbl_Area1Status.InvokeRequired)
{
lbl_Area1Status.Invoke(new Action(() => lbl_Area1Status.Text = "合格"));
lbl_Area1Status.Text = "合格";
lbl_Area1Status.BackColor = Color.Green;
}
}
else
{
if (lbl_Area1Status.InvokeRequired)
{
lbl_Area1Status.Invoke(new Action(() => lbl_Area1Status.Text = "不合格"));
lbl_Area1Status.Text = "不合格";
lbl_Area1Status.BackColor = Color.Red;
label34.BackColor = Color.Red;
label34.Text = "位置1面积检测超限!!!!";
}
if (label34.InvokeRequired)
{
label34.Invoke(new Action(() => label34.Text = "位置1面积检测超限!!!!"));
}
}
if (areaStatus2 == "OK")
{
if (lbl_Area2Status.InvokeRequired)
{
lbl_Area2Status.Invoke(new Action(() => lbl_Area2Status.Text = "合格"));
lbl_Area2Status.Text = "合格";
lbl_Area2Status.BackColor = Color.Green;
}
}
else
{
if (lbl_Area2Status.InvokeRequired)
{
lbl_Area2Status.Invoke(new Action(() => lbl_Area2Status.Text = "不合格"));
lbl_Area2Status.Text = "不合格";
lbl_Area2Status.BackColor = Color.Red;
label34.BackColor = Color.Red;
label34.Text = "位置2面积检测超限!!!!";
}
if (label34.InvokeRequired)
{
label34.Invoke(new Action(() => label34.Text = "位置2面积检测超限!!!!"));
}
}
if (areaStatus3 == "OK")
{
if (lbl_Area3Status.InvokeRequired)
{
lbl_Area3Status.Invoke(new Action(() => lbl_Area3Status.Text = "合格"));
lbl_Area3Status.Text = "合格";
lbl_Area3Status.BackColor = Color.Green;
}
}
else
{
if (lbl_Area3Status.InvokeRequired)
{
lbl_Area3Status.Invoke(new Action(() => lbl_Area3Status.Text = "不合格"));
lbl_Area3Status.Text = "不合格";
lbl_Area3Status.BackColor = Color.Red;
label34.BackColor = Color.Red;
label34.Text = "位置3面积检测超限!!!!";
}
if (label34.InvokeRequired)
{
label34.Invoke(new Action(() => label34.Text = "位置3面积检测超限!!!!"));
}
}
if (lbl_CheckNum.InvokeRequired)
{
lbl_CheckNum.Invoke(new Action(() => lbl_CheckNum.Text = (okNum + ngNum).ToString()));
lbl_CheckNum.Invoke(new Action(() => label11.Text = okNum.ToString()));
lbl_CheckNum.Invoke(new Action(() => label14.Text = ngNum.ToString()));
lbl_CheckNum.Text = (okNum + ngNum).ToString();
label11.Text = okNum.ToString();
label14.Text = ngNum.ToString();
double zs = Double.Parse((okNum + ngNum).ToString());
double OKs = Double.Parse(okNum.ToString());
lbl_CheckNum.Invoke(new Action(() => lbl_OkRate.Text = (OKs / zs).ToString("0.00%")));
lbl_OkRate.Text = (OKs / zs).ToString("0.00%");
}));
//≡≡≡≡≡≡≡≡≡≡ ↑↑↑↑↑↑↑↑↑↑ ≡≡≡≡≡≡≡≡≡≡ ↑↑↑↑↑↑↑↑↑↑ ≡≡≡≡≡≡≡≡≡≡ ↑↑↑↑↑↑↑↑↑↑ ≡≡≡≡≡≡≡≡≡≡【END】【优化Invoke】
}
try
{
modbusClient.WriteSingleRegister("4", (short)modbusResult);//ModbusResult