C:\Program Files\Cognex\VisionPro\samples\Programming\QuickBuild\EfficientApp\C#
C:\Program Files\Cognex\VisionPro\samples\Programming\QuickBuild\mySavedQB.vpp
创建一个Windows窗体应用程序(.NET Framework)
懒得一个一个分辨了,我全都添加进来。
同时,下面这两句不能落下:
using Cognex.VisionPro;
using Cognex.VisionPro.QuickBuild;
//定义变量
CogJobManager m_jobManager;
CogJob m_job;
CogJobIndependent m_jobIndependent;
//初始化
m_jobManager = (CogJobManager)CogSerializer.LoadObjectFromFile(@"C:\Program Files\Cognex\VisionPro\samples\Programming\QuickBuild\mySavedQB.vpp");
m_job = m_jobManager.Job(0);
m_jobIndependent = m_job.OwnedIndependent;
//刷新队列
m_jobManager.UserQueueFlush();
m_jobManager.FailureQueueFlush();
m_job.ImageQueueFlush();
m_jobIndependent.RealTimeQueueFlush();
//结果处理
m_jobManager.UserResultAvailable += M_jobManager_UserResultAvailable;
private void M_jobManager_UserResultAvailable(object sender, CogJobManagerActionEventArgs e)
{
//用户代码
ICogRecord topRecord = m_jobManager.UserResult();
ICogRecord tmp = topRecord.SubRecords[@"Tools.Item[""CogBlobTool1""].CogBlobTool.Results.GetBlobs().Count"];
//throw new NotImplementedException();
}
private void ButtonRunOnce_Click(object sender, EventArgs e)
{
try
{
m_jobManager.Run();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
//throw;
}
}
当上一次处理没有完成,就又立即点击运行按钮,就会出现下列异常。
当然了,我们可以使用try catch语句来抛出异常
delegate void UserResultDelegate(object sender, CogJobManagerActionEventArgs e);
//用户可用的事件处理程序
private void M_jobManager_UserResultAvailable(object sender, CogJobManagerActionEventArgs e)
{
if (InvokeRequired)
{
BeginInvoke(new UserResultDelegate(M_jobManager_UserResultAvailable), new object[] { sender, e });
return;
}
//用户代码
ICogRecord topRecord = m_jobManager.UserResult();
ICogRecord tmp = topRecord.SubRecords[@"Tools.Item[""CogBlobTool1""].CogBlobTool.Results.GetBlobs().Count"];
//throw new NotImplementedException();
int count = (int)tmp.Content;
textBoxCount.Text = count.ToString();
}
private void CheckBoxRunContinue_CheckedChanged(object sender, EventArgs e)
{
if (checkBoxRunContinue.Checked)
{
try
{
m_jobManager.RunContinuous();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
buttonRunOnce.Enabled = false;
}
else
{
try
{
m_jobManager.Stop();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
buttonRunOnce.Enabled = true;
}
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
//窗口关闭
m_jobManager.UserResultAvailable -= M_jobManager_UserResultAvailable;//取消事件关联
cogRecordDisplay1.Dispose();
m_jobManager.Shutdown();
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Cognex.VisionPro;
using Cognex.VisionPro.QuickBuild;
namespace FirstVP
{
delegate void UserResultDelegate(object sender, CogJobManagerActionEventArgs e);
public partial class Form1 : Form
{
//定义变量
CogJobManager m_jobManager;
CogJob m_job;
CogJobIndependent m_jobIndependent;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//初始化
m_jobManager = (CogJobManager)CogSerializer.LoadObjectFromFile(@"C:\Program Files\Cognex\VisionPro\samples\Programming\QuickBuild\mySavedQB.vpp");
m_job = m_jobManager.Job(0);
m_jobIndependent = m_job.OwnedIndependent;
//刷新队列
m_jobManager.UserQueueFlush();
m_jobManager.FailureQueueFlush();
m_job.ImageQueueFlush();
m_jobIndependent.RealTimeQueueFlush();
//结果处理
m_jobManager.UserResultAvailable += M_jobManager_UserResultAvailable;
}
//用户可用的事件处理程序
private void M_jobManager_UserResultAvailable(object sender, CogJobManagerActionEventArgs e)
{
if (InvokeRequired)
{
BeginInvoke(new UserResultDelegate(M_jobManager_UserResultAvailable), new object[] { sender, e });
return;
}
//用户代码
ICogRecord topRecord = m_jobManager.UserResult();
ICogRecord tmpRecord = topRecord.SubRecords[@"Tools.Item[""CogBlobTool1""].CogBlobTool.Results.GetBlobs().Count"];
//throw new NotImplementedException();
int count = (int)tmpRecord.Content;
textBoxCount.Text = count.ToString();
//显示图像
tmpRecord = topRecord.SubRecords["ShowLastRunRecordForUserQueue"];
tmpRecord = tmpRecord.SubRecords["LastRun"];
tmpRecord = tmpRecord.SubRecords["Image Source.OutputImage"];
cogRecordDisplay1.Record = tmpRecord;
cogRecordDisplay1.Fit(true);
}
private void ButtonRunOnce_Click(object sender, EventArgs e)
{
try
{
m_jobManager.Run();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
//throw;
}
}
private void CheckBoxRunContinue_CheckedChanged(object sender, EventArgs e)
{
if (checkBoxRunContinue.Checked)
{
try
{
m_jobManager.RunContinuous();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
buttonRunOnce.Enabled = false;
}
else
{
try
{
m_jobManager.Stop();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
buttonRunOnce.Enabled = true;
}
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
//窗口关闭
m_jobManager.UserResultAvailable -= M_jobManager_UserResultAvailable;//取消事件关联
cogRecordDisplay1.Dispose();
m_jobManager.Shutdown();
}
}
}
C#与VisionPro联合编程.zip