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. replace into 浅析之一

    一 介绍  在笔者支持业务过程中,经常遇到开发咨询replace into 的使用场景以及注意事项,这里做个总结.从功能原理,性能和注意事项上做个说明.二 原理2.1 当表中存在主键但是不存在唯一建的 ...

  2. 14.7.1.2 MySQL创建用户句法

    14.7.1.2 CREATE USER Syntax 单击此处查看原文 以下是MySQL 5.7.6及以上版本的CREATE USER句法 CREATE USER [IF NOT EXISTS] u ...

  3. Android技术之-------电脑获取手机截图

    入吾QQ群183435019(学习 交流+唠嗑) 其实这个相当简单 应该会点ADB命令的人都会吧 一 下载一个ADB 如果你是学Android编程的,,Android SDK应该有吧,在Android ...

  4. 全面总结: Golang 调用 C/C++,例子式教程

    作者:林冠宏 / 指尖下的幽灵 掘金:https://juejin.im/user/587f0dfe128fe100570ce2d8 博客:http://www.cnblogs.com/linguan ...

  5. pm am 12小时格式化

    <div id="m_tiem">PM 14:26</div> tiem('m_tiem'); //pm am 12小时格式化function tiem(i ...

  6. BZOJ 2222: [Cqoi2006]猜数游戏【神奇的做法,傻逼题,猜结论】

    2222: [Cqoi2006]猜数游戏 Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 604  Solved: 260[Submit][Status ...

  7. Codeforces 626F Group Projects(滚动数组+差分dp)

    F. Group Projects time limit per test:2 seconds memory limit per test:256 megabytes input:standard i ...

  8. hdu_1017(水水水,坑格式)

    #include<cstdio> #include<cmath> using namespace std; int main() { int T; scanf("%d ...

  9. [国嵌笔记][020][ARM家族大检阅]

    ARM芯片:2440(arm9) 6410(arm11) 210(cortex-A8) ARM核:arm9(arm-v4) arm11(arm-v6) cortex-A8(arm-v7) 指令架构:a ...

  10. SDP(1):ScalikeJDBC-基本操作介绍

    简单来说:JDBC是一种开放标准的跨编程语言.跨数据库类型编程API.各类型数据库产品厂商都会按它的标准要求来提供针对自身产品的JDBC驱动程序.最主要的这是一套成熟的工具,在编程人员中使用很普及.既 ...