1) Creating a Run-Once Button

通过JobManager调用VisionPro文件。所有的过程放到一个Try/Catch块中。

Private Sub RunOnceButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RunOnceButton.Click         Try             myJobManager.Run()         Catch ex As Exception             MessageBox.Show(ex.Message)         End Try     End Sub 多次点击Button会发现有错误:CogNotStopperException。原因是我们是异步调用VisionPro的,要等到程序结束之后才可以继续调用。

2) Handling Job Manager Events

防止用户在程序未完时再次调用:

Private Sub RunOnceButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RunOnceButton.Click         Try            RunOnceButton.Enabled = False             myJobManager.Run()         Catch ex As Exception             MessageBox.Show(ex.Message)         End Try     End Sub 同时需要在Job完成时通知用户Button可以再次使用了。于是添加JobManager的Stopped事件:

Private Sub myJobManager_Stopped(ByVal sender As Object, ByVal e As CogJobManagerActionEventArgs)         RunOnceButton.Enabled = True     End Sub

接下来如何让Job manager知道有这么一个事件在等他:利用Addhandler来注册事件的句柄:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        myJobManager = CType(CogSerializer.LoadObjectFromFile("C:\Program Files\Cognex\VisionPro\Samples\Programming\QuickBuild\advancedAppOne.vpp"), CogJobManager)         myJob = myJobManager.Job(0)         myIndependentJob = myJob.OwnedIndependent
        myJobManager.UserQueueFlush()         myJobManager.FailureQueueFlush()         myJob.ImageQueueFlush()         myIndependentJob.RealTimeQueueFlush()
        AddHandler myJobManager.Stopped, AddressOf myJobManager_Stopped     End Sub 当注册事件的句柄时,在程序关闭时需要移除句柄:

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing         RemoveHandler myJobManager.Stopped, AddressOf myJobManager_Stopped         myJobManager.Shutdown()     End Sub 当我们运行以上程序时还会有错误:Cross-thread operation not valid。

3)Handling Cross-Thread Function Calls

Remember that the job manager creates several threads. One of these threads fires the Stoppedevent that runs the stopped event handler and eventually attempts to reenable the Run Oncebutton. This sequence of events is illegal because a button -- or any other Microsoft Windows Forms control -- can only be accessed by the thread that created that control. The button was not created by any of the job manager threads.

原因是Job manager在运行时同时创建了几个线程。当其中某一个线程完成时也会触发Stopped事件,这时显示的“Run Once”按钮可用是虚假的。如何保证在真正完成时触发,可考虑用Delegate,委托是类型安全的。

在Stopped事件句柄之前添加:

Delegate Sub myJobManagerDelegate(Byval sender As Object, ByVal e As CogJobManagerActionEventArgs)

检查句柄是否为错误的线程调用,如果是的话就利用委托创建句柄的指针,再用Invoke循环调用:

Private Sub myJobManager_Stopped(ByVal sender As Object, ByVal e As CogJobManagerActionEventArgs)         If InvokeRequired Then             Dim myDel As New myJobManagerDelegate(AddressOf myJobManager_Stopped)             Dim eventArgs() As Object = {sender, e}
            Invoke(myDel, eventArgs)             Return         End If
        RunOnceButton.Enabled = True     End Sub

Visionpro学习笔记 :QuickBuild-Based Application Run-Once Button的更多相关文章

  1. Visionpro学习笔记(壹)

    注册4年,第一次发了随笔.我的博客将主要涉及到visionPro软件的学习,labview数据采集方面的思考,c#及VS的学习 此随笔系列主要是关于VisionPro(以后简称VP)的学习及使用. 近 ...

  2. Spark学习笔记1:Application,Driver,Job,Task,Stage理解

    看了spark的原始论文和相关资料,对spark中的一些经常用到的术语做了一些梳理,记录下. 1,Application application(应用)其实就是用spark-submit提交的程序.比 ...

  3. VisionPro学习笔记:用IEEE1394相机抓取图像

    1)找到采集卡: CogFrameGrabber1394DCAMs cameras = new CogFrameGrabber1394DCAMs(); 2)列举相连接的相机: ICogFrameGra ...

  4. php学习笔记之动态生成一组单选button

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. Self-Host c#学习笔记之Application.DoEvents应用 不用IIS也能執行ASP.NET Web API

    Self-Host   寄宿Web API 不一定需要IIS 的支持,我们可以采用Self Host 的方式使用任意类型的应用程序(控制台.Windows Forms 应用.WPF 应用甚至是Wind ...

  6. [原创]java WEB学习笔记47:Servlet 监听器简介, ServletContext(Application 对象), HttpSession (Session 对象), HttpServletRequest (request 对象) 监听器,利用listener理解 三个对象的生命周期

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

  7. 在Ubuntu上为Android系统内置Java应用程序测试Application Frameworks层的硬件服务(老罗学习笔记6)

    一:Eclipse下 1.创建工程: ---- 2.创建后目录 3.添加java函数 4.在src下创建package,在package下创建file 5.res---layout下创建xml文件,命 ...

  8. [原创]java WEB学习笔记15:域对象的属性操作(pageContext,request,session,application) 及 请求的重定向和转发

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

  9. Spring boot 官网学习笔记 - Spring Boot 属性配置和使用(转)-application.properties

    Spring Boot uses a very particular PropertySource order that is designed to allow sensible overridin ...

随机推荐

  1. 使用JavaScript 操作本地文件

    一.显示用户选择文件[图片] <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...

  2. 浅谈我的MongoDB学习(二)

    上一篇简单讲了mongodb的安装,mongo的windows服务安装,这样服务器重启windows服务会自动重启mongodb的server,然后我们就可以用客户端去管理数据了.mongodb客户端 ...

  3. 手把手教你构建 Kubernetes 1.8 + Flannel 网络(一)

    一.环境说明 操作系统:CentOS7 Kubernetes版本:v1.8.4 Docker版本:v17.06-ce Flannel 版本: flannel-v0.9.1 二.Ntp 服务器配置   ...

  4. 如何迭代输出某文件夹下的所有文件的路径?(os.walk用法)

    查看目录结构: tree 查看文件结构: os.walk 查看os.walk用法: help(os.walk) For each directory in the directory tree roo ...

  5. spring 实现邮箱发送

    使用spring mail 实现的邮箱发送功能,包括附件的发送(附件发送要保证附件存在的路径是真实),使用maven集成jar包,通过spring mvc 实现前后台的调用,发送方使用的是163邮箱, ...

  6. vb代码之------画一个半透明矩形

    入吾QQ群183435019 (学习 交流+唠嗑). 废话不说,咱们来看代码吧 程序结果运行如下 需要如下API 1:GdipCreateFromHDC 功能:创建设备场景相对应的绘图区域(相当于给设 ...

  7. 设备树的interrupt

    http://www.cnblogs.com/targethero/p/5080499.html https://www.cnblogs.com/xiaojiang1025/p/6131381.htm ...

  8. bzoj:1699;poj 3264: [Usaco2007 Jan]Balanced Lineup排队

    Description 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置 ...

  9. dp水一天

    水一些dp的联系题 标签: dp ###hdu_2045 题意 一穿珠子,用三种颜色染色,要求相邻的珠子和两端的珠子不能是同一种颜色,求当有n个珠子的时候有几种染色方案 题解 表示dp[i][j][k ...

  10. c# excel 导入 与 导出(可直接用)

    c#操作excel方式很多 采用OleDB读取EXCEL文件: 引用的com组件:Microsoft.Office.Interop.Excel.dll   读取EXCEL文件 将EXCEL文件转化成C ...