NI TestStand

cancel
Showing results for 
Search instead for 
Did you mean: 

How to instantiate a .net assembly after loading a sequence?

I have a .net library with a lot of code, it takes time to do some initializing while constructing an instance of the class. I would like to call the constructor of the class after loading the sequence. Within the sequence, I have to call methods from the same instance of the class.

 

Does anyone know how to do this?

 

Thanks

0 Kudos
Message 1 of 13
(6,807 Views)

Hi,

 

you could use the EngineCallback SequenceFileLoad.

But I think this not a good solution, because when you open the File just for editing stuff the constructor will be executed.

and have to use the StationGlobals for storing the Object. (i have not tried this feature!)

I recommon to use the ModelCallbacks ProcessSetup/Cleanup

This will be call at first when pressing "Start".

 

I have modified the Demo/Dotnet/Comuter-Example that comes with TS

Just try it. Note: you have to copy it to you Ni-Computer Example Folder.

 

Hope this helps

 

juergen

--Signature--
Sessions NI-Week 2017 2016
Feedback or kudos are welcome
0 Kudos
Message 2 of 13
(6,791 Views)

There is a feature of the dotnet adapter specifically for doing this.

 

1) Create a new dotnet step or use the first dotnet step in your sequence at the place where you will first need the instance of your class.

2) Configure the step to create and store an instance of your class.

3) Go to the Customize Constructor specification panel or dialog for the step

4) Check the "Use Step Load/Unload Options to Specify Object Creation Time and Lifetime" checkbox

5) Go to the Run Options for the step and change the step's Load Option to "Preload when opening sequence file"

6) Change the Unload Option to specify the behavior you desire (i.e. you can keep and reuse the same objects for multiple runs of your execution and step or you can unloaded it sooner based on your setting for this run option)

 

NOTE: The variable you specified to store the instance of the class object in step 2) above will get assigned the class object at runtime when your step executes, however the object itself will really be created based on the load/unload options you specify.

 

NOTE 2: You only need to configure one step to do the creation and have that step store the instance in a variable. Your other steps that you want to use the same instance should just specify the variable as the object reference for the class and not do any creating.


Hope this helps. Let me know if you need more information or have trouble getting this working.

 

-Doug

Message Edited by dug9000 on 06-22-2009 11:37 AM
Message 3 of 13
(6,786 Views)
Thanks Doug, this is exactly what I needed!
0 Kudos
Message 4 of 13
(6,778 Views)

Hi Doug,

 

Thanks for your great answer !

I know these step setting. I often use them with the DLL-Adapater.

But till today i was not knowing how to handle it with the instance variable on .net!

 

I think your notes where very important for a lot of .net programmers out there.

 

Juergen

--Signature--
Sessions NI-Week 2017 2016
Feedback or kudos are welcome
0 Kudos
Message 5 of 13
(6,772 Views)

Hi dug9000, can you site an example of this one?

I am fairly new at teststand and i am having a hard time passing .NET object to sequence.

Any help will be appreciated a lot! 

0 Kudos
Message 6 of 13
(6,622 Views)

Calling a .NET class and creating a .NET object is done by using the .NET adapter. It's one of the icons at the top of the step insert palette (look at the tooltips). Once you select the .NET adapter. Just insert the step type you want to use such as "Action", or "Numeric Limit". Then configure the call in the step settings panel at the bottom of the screen. If you are more specific about what you are having trouble with perhaps I can help more.

 

-Doug

0 Kudos
Message 7 of 13
(6,603 Views)

Sorry for delayed reply - Ive been out for vacation.

Anyway, I figure that part out.

 

Here's a new problem.I am encountering error "The .NET parameter "i" could not be accessed."

Unable to cast object of type to Instrument to Instrument.

Error code:-17500.

 

Scenario is I'm passing object from .NET to my sequence. 

I never had problem if I'm passing string or numeric but whenever I am passing object I am encountering the error above.

 

Below is my .NET code:

   SequenceFile sqf =e.exec.GetSequenceFile();  ;

            PropertyObjectFile pof = sqf.AsPropertyObjectFile();

 

 

            PropertyObject po = sqf.GetSequence(0).AsPropertyObject();

 

  StationSetup stationSetup = new StationSetup();

stationSetup.SpecialInstructions="instructions";

Instrument instrument = new Instrument();

instrument.Name = "sample";

instrument.Command = "myCommand"; 

stationSetup.Instrument = instrument;

 

 

            if (po.Exists("Locals.objStationSetup", 0))

            {

                po.SetValInterface("Locals.objStationSetup", 0, stationSetup);

 

            }

 

            if (po.Exists("Locals.objInstrument", 0))

            {

 

 

 

                po.SetValInterface("Locals.objInstrument", 0, instrument);

            }

 

 

            if (po.Exists("Locals.strMessage", 0))

            {

                po.SetValString("Locals.strMessage", 0, "message from net");

            }

 

 

 Attached is the sequence and dll that I am using.

 

Additional info: When I am running my sequence in my testand it is just running fine but whenever I am running it from my .NET it is throwing me an error.

 

Message Edited by -mbda- on 08-14-2009 04:58 PM
0 Kudos
Message 8 of 13
(6,541 Views)

Is the Instrument object a .NET object or a COM object? One issue is that the SetValueInterface() method of PropertyObject is expecting a COM object, not a .NET object, so what it stores for a .NET object is actually a COM wrapper around the .NET object that the .NET framework likely created for you. I suspect the .NET adapter doesn't know how to convert this back to the .NET version of the type. If you want to store .NET objects in a TestStand Object Reference variable in a way that the .NET adapter will be able to pass it in to .NET code as a .NET object, I recommend passing it back as an output parameter and letting the adapter store it in a variable rather than using SetValueInterface.

 

 

Hopet this helps,

-Doug

0 Kudos
Message 9 of 13
(6,517 Views)

Yes, Instrument is a .NET object.

 

When you say passing it as output parameter, I am going to create method with Instrument output parameter? I did that, see attached file.

It is an ok work around, it is running fine insing teststand but it is throwing me similar error (the one that I originally reported) when I am running it in .NET framework.

 

Here is the method that has output parameter:

 

 public void LoadAnotherObject(out Instrument ins)

        {

            Instrument i = new Instrument();

            i.Name = "sample1 from ss";

            i.Command = "boom boom pow";

 

            instrument = i;

 

            ins = i;

        } 

 

If I misunderstood what you are saying, can you please give me an example? 

 

Thanks! 

 

0 Kudos
Message 10 of 13
(6,473 Views)