LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Atomic class methods with .NET Controller in CVI 8.0.0

Hello everybody,
I wrote a DOTNET class (with Visual Studio Express 2005) and with CVI 8.0 I created the NET Controller.
Ok it's fine, but there's a (big) problem in multi thread scenario.
Calling the method in separated threads, they are executed 'atomicly'
I mean, the thread instances of the same method are executed in a 'serial mode', not in parallel.
I think may be the "CVI NET controller" was designe with blocking criteria calling his methods. (??!!??)
Is it true?
Anybody (with same problem, or National Instrument developers) can help me??
Thanx
 
Fabrizio
0 Kudos
Message 1 of 10
(4,441 Views)
Hi Fabrizio,

I'm having a hard time understanding exactly what your issue is.

From what I can gather from your post, you have a .NET assembly with some method. Then you are calling this method in the main UI thread and another thread you created in CVI? Is that a correct assumption? If not, please correct me so I have a clear picture.

If that is the case, have you tried comparing our behavior to how .NET handles that scenario? In that I mean, in .NET (C#, VB.NET, whatever language you like), call that assembly's method both in the main UI thread and some thread you created. See what happens!

Now, these suggestions might not matter if you are talking about a completely different issue so please let me know.

Best Regards,
Jonathan N.
National Instruments
0 Kudos
Message 2 of 10
(4,424 Views)
Hi Jonathan,
I'm really sorry for my very bad English! I still studing it!
I'll try to explain better the problem:
1) I made a dotnet (managed) dll with a class and methods with MS Visual Studio Express 2005.
2) I want to call it from a CVI 8 project. I want to create more instance of my class and execute my method in separated threads. So, I want to execute parallely the same method in differet thread. (that's the problem!)
3) I made the Net Controller wrapper with CVI tool. It's necessary to use my dll in a 'unmanaged' code (my CVI project).
4) Now, in CVI I create many threads (with win32 function CreateThread(..)) and inside each thread I create and call my methods (via CVI net controller).
As result the execution of my dotnet class methods are serialized. I mean the parallel execution of the methods (and precisely only the functions wrapper) are made not in parallel but in serial.
It's seem to be some mechanism like 'lock' or whatever inside the CVI function wrapper of my dotnet class that made the execution of the wrapper functions Atomicly.
I tested my dotnet class in a native dotnet project and threads and it's working in real parallel execution.
I have CVI version 8.0.0.
I hope to be clear.
Many Thanx
 
Fabrizio
0 Kudos
Message 3 of 10
(4,421 Views)
Hi Fabrizio,
 
I tried replicating the behavior you are seeing but I am unable to. The steps I followed were:
 
1) I created a VS 2005 .NET assembly that had one class and one method.
2) Next, I created the C wrapper for this assembly. 
3) I then forked off a new thread which now gives me the main UI thread and secondary thread to work with
4) In the main UI thread, I instantiated the class and called the method over and over again in while a while. During that time, I spit out DebugPrintf messages to the Debug Output window. This allowed me to see if I was actually in the thread and it was actually calling the method
5) In my secondardy thread, I did the same thing of instantiating the class and calling the method. Again, i used the DebugPrintf messages in this thread.
 
When I ran my program, I could see the Debug Output window showing the debug printf messages for both threads. The messages kept alternating which indicates the threads were both executing at the same time and calling those methods. 
 
I have attached the simple .c file I used to test this out. I don't think you need the wrapper or UI to figure out what I was doing.
 
Perhaps my tests are different than yours.
 
Reply back and let me know your thoughts!

Best Regards,
Jonathan N.
National Instruments
0 Kudos
Message 4 of 10
(4,416 Views)

Ok Jonathan,

Thinking at your sample code, try to put in your 'dotnet' method a loop writing out a simple string.

Executing the two threads in parallel time, it shuld be execute interlived (you know what I mean).

Is it true?

My test code in 'managed' method is:

 

int MyClass::TestMethod (void) {

Random^ Rand =

gcnew Random;

int iUnicID = Rand->Next(1000); // for identify the thread

for (iCnt=0; iCnt < 10; iCnt++) {

Thread::Sleep(1000);

printf,

"StdClass::iGetInterfaceId [%d]", iUnicID);

Console::WriteLine(

"StdClass::iGetInterfaceId {0}", iUnicID);

}

}

I saw the serial execution of the methods via the number random generated for single thread.

Thanx a lot for your time!

Fabrizio

0 Kudos
Message 5 of 10
(4,412 Views)
Hi Fabrizio,

I added that loop in my .NET assemblies method and my threads in CVI were still executing in parallel.  I even threw in the Windows SDK GetSystemTime function that allows me to get the current time to the millisecond resolution inside of my CVI threads to tell me when they started. Then I spit out to the debug output window what time the threads started and the timing matched up (i.e. thread 1 started at for example 17:25:11:247 and thread 2 started at 17:25:11:247). This clearly indicates the threads are running in parallel.

So I guess I still not convinced that our code is not truly running threads in parallel.

I'd be happy to run some sample code that you have to perhaps see what you are seeing. If you would, please post a simple CVI project call a basic .NET assembly that demonstrates the behavior.

Best Regards,
Jonathan N.
National Instruments
0 Kudos
Message 6 of 10
(4,404 Views)
Hi Jonathan,
I made a simple managed dll with the sample code and a little cvi project to test my envirounment.
I made 2 threads calling the same 'managed' method (ok we know!).
Afterwards I called in 2 threads the same implementation of the method wrote in 'unmanaged' 'C'.
And the problem is in my screen now... Let you see my source and my txt results inside the zip file.
Note: The cvi project must be in DEBUG configuration due the creation of text console for printing message (I think).
Many thanx for your advices
 
Fabrizio
0 Kudos
Message 7 of 10
(4,395 Views)

Hi Fabrizio,

Thanks for posting that example as it does show the behavior you were talking about. At this point I'm not really sure why the methods are executing serially so I'm going to have to look into this some more. Just to clarify an earlier statement you made, calling these methods from threads in .NET showed parallel execution right?

I'll keep you posted on what I find.

Thanks

Best Regards,

Message Edited by Jonathan N on 06-08-2007 06:02 PM

Jonathan N.
National Instruments
0 Kudos
Message 8 of 10
(4,379 Views)
Ok Jonathan,
Thank you very much.
 
You wrote:
"Just to clarify an earlier statement you made, calling these methods from threads in .NET showed parallel execution right?"
Answer:
Yes in NET envirounment 2 threads execute the same method in parallel. (I tested that)
 
Fabrizio
0 Kudos
Message 9 of 10
(4,356 Views)

Hi Fabrizio,

The reason that you are seeing serial behavior is because the .NET library is made thread-safe at the library level. Therefore each method invocation happens serially. Essentially, there is one lock for all .NET library functions. 

I must have had a brain freeze or something as I totally forgot about this.

Best Regards,

Jonathan N.
National Instruments
0 Kudos
Message 10 of 10
(4,330 Views)