NI TestStand

cancel
Showing results for 
Search instead for 
Did you mean: 

Run a Testcase with a Execution object in a thread /asynchronously C#/.NET

Solved!
Go to solution

Hello everybody,

I try to execute a sequence in my C# Webapplication.

for this I use the following Method:

 

public void ExecuteTest(Engine testStandEngine, SequenceFile sequenceFile)
{

Execution testExecution = testStandEngine.NewExecution(sequenceFile, "MainSequence", null, false, ExecutionTypeMask.ExecTypeMask_Normal);

testcaseExecution.WaitForEndEx(2000, true);

Console.WriteLine("Testcase " + "example" + "in the sequence: " + "Test.seq" + " finished with ResultStatus: " + testcaseExecution.ResultStatus);

}

If I run this code synchronously everything is fine the ResultStatus- property of theExecution object is "Passed".

If I run this code in another Thread the WaitForEndEx- Method gets a Timeout and my ResultStatus Property is empty. The RunState of the execution is also running but it looks like the execution never stops.

Here is the execution with another thread.

System.Threading.Thread executionCaller = new System.Threading.Thread(() =>
{
ExecuteTest(_testStandEngine, _sequenceFile);
});
executionCaller.SetApartmentState(ApartmentState.STA);
executionCaller.Start();

 

Can you help me?

I try to build a service which execute a test after my Service gets a trigger from a communication interface.

 

0 Kudos
Message 1 of 5
(171 Views)

Hi tlkr01,

 

How do you instantiate the Engine object?

 

I think your issue is related to the fact that WaitForEndEx is not handling UI messages coming from your execution. See: Solved: Run sequence in .nET by Teststand API - NI Community

 

Best regards

Leonard

0 Kudos
Message 2 of 5
(97 Views)

Hi Leonard,

sorry for the late reply. Thanks for your suggestion.

 

I have a console application to test the behaviour of the testexecution in another thread:

 

// See https://aka.ms/new-console-template for more information
using NationalInstruments.TestStand.Interop.API;
using NationalInstruments.TestStand.Interop.UI;

Console.WriteLine("Hello, World!");
Engine _testStandEngine = new Engine();
_testStandEngine.UIMessageEvent += msg =>
{
  var events = msg.Event;
};
_testStandEngine.UIMessagePollingEnabled = true;
string _sequencePath = "myPath";
Execution testcaseExecution;
SequenceFile _sequenceFile = _testStandEngine.GetSequenceFileEx(_sequencePath);
System.Threading.Thread mainThread = System.Threading.Thread.CurrentThread;
System.Threading.Thread waitingCaller = new System.Threading.Thread(() =>
{
  bool stop = true;
  while (stop)
  {
    if (!_testStandEngine.IsUIMessageQueueEmpty)
    {
      UIMessage msg = _testStandEngine.GetUIMessage();
      switch (msg.Event)
      {
        case UIMessageCodes.UIMsg_StartExecution:
          // do something
          Console.WriteLine("StartFileExecution");
          break;
        case UIMessageCodes.UIMsg_EndExecution:
          // do something
          Console.WriteLine("ExecutionFinished!");
          break;
        case UIMessageCodes.UIMsg_EndFileExecution:
          // do something
          Console.WriteLine("ExecutionFinished!");
          break;
        case UIMessageCodes.UIMsg_EndInteractiveExecution:
          // do something
          Console.WriteLine("ExecutionFinished!");
          break;
      }
    }
  }
});
waitingCaller.Start();
while (true)
{
  //testcaseExecution = _testStandEngine.NewExecution(_sequenceFile, "MainSequence", null, false, ExecutionTypeMask.ExecTypeMask_Normal);
  //testcaseExecution.WaitForEndEx(2000, true);
  //Console.WriteLine("Testcase " + "example" + "in der TestSequenz: " + "Remote_Test.seq" + " abgeschlossen mit dem ResultStatus: " + testcaseExecution.ResultStatus);
  Console.WriteLine("MainThread: " + System.Threading.Thread.CurrentThread.ManagedThreadId.ToString());
  System.Threading.Thread executionCaller = new System.Threading.Thread(() =>
  {
    testcaseExecution = _testStandEngine.NewExecution(_sequenceFile, "MainSequence", null, false, ExecutionTypeMask.ExecTypeMask_Normal);
    testcaseExecution.WaitForEndEx(2000, true);
    Console.WriteLine("Testcase " + "example" + "in der TestSequenz: " + "Remote_Test.seq" + " abgeschlossen mit dem ResultStatus: " + testcaseExecution.ResultStatus);
  });
  executionCaller.SetApartmentState(ApartmentState.STA);

  executionCaller.Start();
  executionCaller.Join();
  Task.Delay(2500);
}

 

If I run this sourcecode the UIMessage Event occures for the StartExecution message, but not for the EndExecution messages. So it looks like the execution does never finish. The WaitForEndEx- Method also returns false. Do you have another solution for me?

 

Best regards,

 

Thomas

0 Kudos
Message 3 of 5
(64 Views)
Solution
Accepted by topic author tlkr01

Hi Thomas,

 

Good to see you set up an example. When comparing it with the answer from Solved: Run sequence in .nET by Teststand API - NI Community, I noticed two things:

 

1. You forgot to acknowledge the UI messages:

msg.Acknowledge();

 

2. The UI messages should be handled from the main thread of your program.

 

Best regards

Leonard

0 Kudos
Message 4 of 5
(54 Views)

Hi Leonard,

your hint to acknowledge the UI Message solved my issue. Thanks for your help!

 

Best regards,

Thomas

0 Kudos
Message 5 of 5
(51 Views)