Machine Vision

cancel
Showing results for 
Search instead for 
Did you mean: 

accewwing method from another thread causing "wrong type of object" error with C# VS2008

Solved!
Go to solution

I am snapping an image to a main viewer, drawing a rectangle around a fiducial, and extracting that portion of the image to another viewer for further processing. I tested everthing and all was good until I began to build a wizard to walk the operator through the process and do some things automatically. If I access this method from the mainform thread all runs well. If I access it from the wizards background worker thread it fails and gives the error " you supplied the wrong type of object to the function. Pass a CWIMAQRectangle instead."

 

public int ExtractToGP1()

{

CWIMAQLearnPatternOptions LearnOptions = new CWIMAQLearnPatternOptions();

 

CWIMAQRectangle RoiRectangle = new CWIMAQRectangle();

int Count, Result;

 

//Extract region correspoding to the pattern selected in the main viewer

Result = axCWMachineVision1.GetSelectedRectangleFromViewer(axCWIMAQViewer1.GetCWIMAQViewer(), RoiRectangle, true, 1); //<--error here

 

 

Result = axCWIMAQVision1.Extract2(axCWIMAQViewer1.Image, axCWIMAQViewer5.Image, RoiRectangle, 0, 0);

 

 

I tried using:

axCWMachineVision1.Invoke((MethodInvoker)delegate

{ Result = axCWMachineVision1.GetSelectedRectangleFromViewer(axCWIMAQViewer1.GetCWIMAQViewer(), RoiRectangle, true, 1); });

 

 

axCWMachineVision1.Invoke((MethodInvoker)delegate

{ Result = axCWIMAQVision1.Extract2(axCWIMAQViewer1.Image, axCWIMAQViewer5.Image, RoiRectangle, 0, 0); });

 

but this also only works if the method is called from the main thread.

 

0 Kudos
Message 1 of 2
(3,312 Views)
Solution
Accepted by topic author bob13873

Solved this myself. Not really an NI issue.

 

I moved

CWIMAQLearnPatternOptions LearnOptions = new CWIMAQLearnPatternOptions();

CWIMAQRectangle RoiRectangle = new CWIMAQRectangle();

to my vision class instead of having it in the method, which is in the mainform class

then use Vision.RoiRectangle vs RoiRectangle and Vision.LearnOptions vs LearnOptions.

0 Kudos
Message 2 of 2
(3,308 Views)