C3204
2025-12-23 6ef4ced299197b0a8480734d3fda9e32b38e7a9a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
using HalconDotNet;
using LB_SmartVision.ProcessRun;
using LB_VisionControl;
using Layout = LB_SmartVision.Forms.Pages.SettingPage.Layout;
 
namespace LB_SmartVision.Forms.Pages.ProcessPage
{
    public partial class ProcessControl : UserControl
    {
        UserHSmartWindowControl UserHSmartWindowControl = new UserHSmartWindowControl();
 
        ProcessRunBll ProcessRunBll
        {
            get
            {
                if (GlobalVar.dicProcesses.ContainsKey(_Layout.ProcessName))
                    return GlobalVar.dicProcesses[_Layout.ProcessName];
                else
                    return null;
            }
        }
 
        Layout _Layout { get; set; } = new Layout();
 
        public ProcessControl()
        {
            InitializeComponent();
        }
 
        public ProcessControl(Layout layout)
        {
            InitializeComponent();
            _Layout = layout;
        }
 
        private void ProcessControl_Load(object sender, EventArgs e)
        {
            this.panel1.Controls.Add(this.UserHSmartWindowControl);
            this.UserHSmartWindowControl.Dock = DockStyle.Fill;
 
            SetTitle(_Layout.ProcessName);
 
            if (ProcessRunBll != null)
                this.label1.Text = $"总数:{ProcessRunBll.total}";
        }
 
        public void SetTitle(string title)
        {
            // 如果当前不是 UI 线程,则通过 Invoke 将操作调度到 UI 线程
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new Action<string>((title) =>
                {
                    this.lblTitle.Text = title;
                }), title);
            }
            else
            {
                // 如果已经在 UI 线程上,直接更新 
                this.lblTitle.Text = title;
            }
        }
 
        public void ClearObj()
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new Action(() =>
                {
                    UserHSmartWindowControl.ClearObj();
                }));
            }
            else
                UserHSmartWindowControl.ClearObj();
        }
 
        /// <summary>
        /// 异步显示图片
        /// </summary>
        /// <param name="ho_image"></param>
        public void ShowHoImage(HObject ho_image)
        {
            UserHSmartWindowControl.hImage = ho_image;
 
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new Action(() =>
                {
                    this.label1.Text = $"总数:{ProcessRunBll.total}";
                }));
            }
            else
                this.label1.Text = $"总数:{ProcessRunBll.total}";
        }
 
        public bool Run(out string msg)
        {
            if (ProcessRunBll == null)
            {
                msg = $"流程{_Layout.ProcessName}不存在";
                return false;
            }
 
            if (isCircleRuning || ProcessRunBll.bRuning)
                ProcessRunBll.LogInfo($"{ProcessRunBll.Name}正在运行", LogInfoType.ERROR);
 
            ProcessRunBll.Run();
 
            msg = ProcessRunBll.Msg;
            return ProcessRunBll.Result;
        }
 
        private void btnRun_Click(object sender, EventArgs e)
        {
            this.BeginInvoke(new Action(() =>
            {
                string msg = string.Empty;
                bool result = false;
                try
                {
                    if (ProcessRunBll == null)
                        return;
 
                    ProcessRunBll.LogInfo(string.Format("流程[{0}]开始运行", this.ProcessRunBll.Name), LogInfoType.INFO);
 
                    ClearObj();
                    result = Run(out msg);
 
                    if (ProcessRunBll.GetImage(_Layout, out _, out HObject RecordImage))
                        ShowHoImage(RecordImage);
                }
                catch { msg = "运行出现了异常"; }
 
                ProcessRunBll.LogInfo(string.Format("流程[{0}]运行完成,结果:{1}", this.ProcessRunBll.Name, msg)
                , result ? LogInfoType.PASS : LogInfoType.ERROR);
            }));
        }
 
 
        CancellationTokenSource ctsTask = new CancellationTokenSource();
        bool isCircleRuning = false;
        private void btnCircleRun_Click(object sender, EventArgs e)
        {
            try
            {
                isCircleRuning = !isCircleRuning;
                btnRun.Enabled = !isCircleRuning;
 
                if (isCircleRuning)
                {
                    threadCircleRun = new Thread(ThreadCircleRun);
                    threadCircleRun.IsBackground = true;
                    threadCircleRun.Start();
                }
                else
                {
                    isCircleRuning = false;
                    threadCircleRun.Abort();
                }
            }
            catch { }
        }
 
        Thread threadCircleRun = null;
 
        void ThreadCircleRun()
        {
            if (!isCircleRuning)
            {
                ProcessRunBll.LogInfo($"{ProcessRunBll.Name}关闭连续运行", LogInfoType.PASS);
                return;
            }
 
            ProcessRunBll.LogInfo($"{ProcessRunBll.Name}开启连续运行", LogInfoType.WARN);
 
            while (isCircleRuning)
            {
                //this.BeginInvoke(new Action(() =>
                //{
                try
                {
                    ProcessRunBll.LogInfo($"{ProcessRunBll.Name}开始运行", LogInfoType.WARN);
                    ClearObj();
                    bool result = ProcessRunBll.Run();
                    string msg = result ? "运行成功" : ProcessRunBll.Msg;
 
                    ProcessRunBll.LogInfo($"{ProcessRunBll.Name}运行结束,结果为:{msg}"
                        , result ? LogInfoType.PASS : LogInfoType.ERROR);
 
                    if (ProcessRunBll.GetImage(_Layout, out _, out HObject RecordImage))
                        ShowHoImage(RecordImage);
                }
                catch { }
                //}));
 
                Thread.Sleep(100);
            }
        }
 
        System.Windows.Forms.ToolTip TotalToolTip = new System.Windows.Forms.ToolTip();
 
        private void lblCount_DoubleClick(object sender, EventArgs e)
        {
            this.BeginInvoke(() =>
            {
                ProcessRunBll.ClearTotal();
                this.label1.Text = $"总数:{ProcessRunBll.total}";
            });
        }
 
        private void lblCount_MouseHover(object sender, EventArgs e)
        {
            // 显示ToolTip(相对于控件的位置)
            TotalToolTip.Show($"直通率:{ProcessRunBll.Rate_OK.ToString("F3")}", (Control)sender,
                               ((Control)sender).Width / 2, // x偏移
                               -20,                      // y偏移(负值表示上方)
                               2000);                    // 显示时间(ms)
        }
    }
}