04-17-2025 03:34 AM
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.
Solved! Go to Solution.
04-23-2025 05:49 PM
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
04-28-2025 03:59 AM
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
04-28-2025 04:43 AM
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
04-28-2025 05:01 AM
Hi Leonard,
your hint to acknowledge the UI Message solved my issue. Thanks for your help!
Best regards,
Thomas