Visionpro Study notes: quickbuild-based application run-once Button

Source: Internet
Author: User

1) Creating a run-once Button

The Visionpro file is called through JobManager. All the processes are put into a try/catch block.

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 Multiple clicks on button will find an error: Cognotstopperexception. The reason is that we call Visionpro asynchronously, and we wait until the end of the program before we can continue the call.

2) Handling Job Manager Events

Prevent users from calling again when the program is not finished:

Private Sub Runoncebutton_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Runoncebutton.clic K         try              runoncebutton.enabled = False             myjobmanager.run ()         catch ex as Exception              messagebox.show (ex. Message)         end Try      end Sub also needs to notify the user that the button can be used again when the job is completed. Then add the JobManager stopped event:

Private Sub myjobmanager_stopped (ByVal sender as Object, ByVal e as Cogjobmanageractioneventargs) Runoncebutto n.enabled = True End Sub

Next, how to let job manager know that there is an event waiting for him: Use AddHandler to register the handle of the event:

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)        < Span class= "apple-converted-space" > myindependentjob = myjob.ownedindependent
         myjobmanager.userqueueflush ()          myjobmanager.failurequeueflush ()          myjob.imagequeueflush ()          myindependentjob.realtimequeueflush ()
       &NBSP; AddHandler myjobmanager.stopped, AddressOf myjobmanager_stopped      end Sub when registering the handle to the event, you need to remove the handle when the program closes:

Private Sub form1_formclosing (ByVal sender as Object, ByVal e as System.Windows.Forms.FormClosingEventArgs) Handles Me.fo Rmclosing removehandler myjobmanager.stopped, AddressOf myjobmanager_stopped Myjobmanager . Shutdown () End Sub When we run the above program there will also be an error: Cross-thread operation not valid.

3 ) Handling Cross-thread Function Calls

Remember that Job Manager creates several threads. One of these threads fires the Stoppedevent that runs the Stopped event handler and eventually attempts to re Enable the Run Oncebutton. This sequence of events are illegal because a button--or any other Microsoft Windows Forms control--can only be Accesse D by the thread, which created that control. The button is not a created by any of the job manager threads.

The reason is that Job manager creates several threads at run time. The stopped event is also triggered when one of the threads finishes, and the "Run Once" button that is displayed can be false. How to ensure that it is triggered when it is really complete, consider using delegate, which is type-safe.

Before the stopped event handle, add:

Delegate Sub myjobmanagerdelegate (Byval sender as Object, Byval e as Cogjobmanageractioneventargs)

Check if the handle is called by the wrong thread, and if it is, use the delegate to create a handle pointer and invoke the Invoke loop:

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 Study notes: quickbuild-based application run-once Button

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.