Visionpro学习笔记 :QuickBuild-Based Application Run-Once Button

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

 

转载于:https://www.cnblogs.com/amosyang/p/4946862.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值