LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Problem calling more than one instance of a dll doing a InetTelnetOpen

Hi,
I have made a DLL with the evaluation version of LabWindows 7.1 to connect to a Telnet server and perform various commands. This DLL is to be used with TestStand 3.1 . In TestStand, I have to connect simultaneously to the same Telnet server twice to start an application with both of those connection, with different parameters. I use Threads in TestStand to call the DLL an everything seems to be fine on that side. I enter the DLL at the same time for both threads but it seems that the 1st thread waits until the 2nd thread is at the end of the function before he executes the InetTelnetOpen command to start the Telnet session. Is it normal that we can only have one Telnet connection at a time?

To see that I've placed some time stamps in a log. For the first Thread, almost a minute passes by between the first 2 time stamps but in the 2nd thread it take less than a second.

Yet, they both wait for eachother to exit the DLL as the time stamp at the end of the execution is the same.

I really need some enlightenment here 😉

Louis
0 Kudos
Message 1 of 18
(5,080 Views)
Hello TiWi,

I suspect what is happening is that the code in one thread is block the code in the other thread at the InetTelnetRunScript command. What is the TimeOut value you are passing to this function? The default timeout value is 25000 ms (25s). If you reduce the timeout to 50ms, do you see behavior closer to what you expect?

Thanks.
Wendy L
LabWindows/CVI Developer Newsletter
0 Kudos
Message 2 of 18
(5,028 Views)
Hello TiWi,

Also, I assume you are already doing this, but I would double check that you are trying to connect to different ports from the two threads.

Thanks.
Wendy L
LabWindows/CVI Developer Newsletter
0 Kudos
Message 3 of 18
(5,027 Views)
You mean local ports or remote ports?
0 Kudos
Message 4 of 18
(5,025 Views)
Hi,

The multithreading safety of many CVI libraries including the Internet library is such that only one thread can execute a function in the library at one time. Basically, each function in the library is protected by a library-level lock. So, only one thread can execute in InetTelnetOpen at one time. Note that after this thread returns from InetTelnetOpen, another thread can call InetTelnetOpen and that should work fine. So, multiple threads can use the library from the same or different DLLs - but only one thread can be executing a library function at any time. Hope this clarifies what you are seeing.

Best regards,
Mohan
0 Kudos
Message 5 of 18
(4,999 Views)
Thank you for the clarification. Is this limitation documented somewhere? After doing further testing, I have identified that it is the InetTelnetRunScript which is locking up the DLL for too long. Is there a workaround to execute 2 InetTelnetRunScript at the same time, or something similar?
0 Kudos
Message 6 of 18
(4,997 Views)
To clarify what I need to do, I want to connect twice to a Telnet server (same server or 2 different servers) and execute 2 instances of the same test application on the remote server, with different parameters for the test applications. Those 2 tests must run pretty much simultaneously. I cannot do this in the same console window since the application will only return once the tests are done.

I've tought of using a compination of write/readuntil commands but then again, I am afraid that one of the readuntil calls in one thread will block the one in the other thread and I will miss the string to find.

Am I right?

Thanks!

Louis
0 Kudos
Message 7 of 18
(4,993 Views)
Hi,

Yes, you are correct about using Read and Write, especially using ReadUntil. ReadUntil would just block all threads until the pattern is reached (or error or timeout occurs) and may not be what you want for your setup. You can try calling Read with a small byte count in a loop and then matching the pattern yourself - this may be the best solution though a little cumbersome. Both the threads should be able to call read in their loops and get the output from both instances of your app concurrently. If you do this, I would recommend starting out with a read byte count of 1, and having a Sleep call in your while loop.

Basic telnet is not a very difficult protocol, and there is some sample code that shows how one can do telnet with the CVI TCP library and get the output in a callback at the following link:
http://sine.ni.com/apps/we/niepd_web_display.display_epd4?p_guid=F741A0C94BC44CABE0340003BA7CCD71

If the read-loop solution does not work out, you may want to try getting the output in different threads using callbacks with the above code. I would recommend the read-loop solution over this because it should be easier to implement and debug compared to this, and callbacks come at a price and might be slower.

Best regards,
Mohan
0 Kudos
Message 8 of 18
(4,966 Views)
Hi,
In terms of local variables and multithreading, do they have to be protected in a certain way? What I am doing with the dll is that I am calling the same function from two threads so does this mean that the variables are created twice at different places in memory, one for each thread?

Thanks for clarifying this,

Louis
0 Kudos
Message 9 of 18
(4,956 Views)
Louis,

You don't need to worry about protecting local variables in multithreaded programs (unless they're declared as static). Non-static locals exist on the stack, and each thread has its own copy of the stack, therefore locals are not shared across threads. You only need to protect access to global variables and static locals.

Luis
NI
0 Kudos
Message 10 of 18
(4,945 Views)