LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Variable passed by ref to DLL doesn't seem to get re-evaluated properly inside while-loop

I am using LabVIEW 8.2.1. and I use the Application Builder to generate DLL-files from LabVIEW VI's. I would like to create a DLL that generates a square wave on my PXI-4461. I used the DAQ assistant to generate the code for the PXI-4461 and since I want a continuous signal, the code is generated inside a while-loop. I have no problems starting my square wave, but I am unsuccessful stopping it. I know for sure that arrays are passed by reference so I decided to pass a stop flag inside an array to the DLL from my (C#) application. However, the value of the stop flag is not updated inside the while-loop in the LabVIEW-code, I confirmed this by adding a message box inside my LabVIEW code. What is wrong with my code? A picture of my VI is attached and the relevant C# code looks like this:

 

    private void testButton_Click(object sender, EventArgs e)

    {

      LabViewInterface.startSendingOutSquareWave();

      System.Threading.Thread.Sleep(3000);

      LabViewInterface.stopSendingOutSquareWave();

      MessageBox.Show("Have now tried to stop the square wave");

    }

 

  public class LabViewInterface

  {

    private static volatile IntPtr stopFlagPtr = Marshal.AllocHGlobal(8);

 

 

    [DllImport("SendOutSquareWave.dll")]

    private static extern void SendOutSquareWave(IntPtr stopArray, int stopLength);

 

 

    public static void startSendingOutSquareWave()

    {

      Marshal.Copy(new double[] { 0 }, 0, stopFlagPtr, 1);

      Thread workerThread = new Thread(delegate()

      {

        unsafe

        { // Call dll

          SendOutSquareWave(stopFlagPtr, 1);

        }

      });

      workerThread.Start();

    }

 

 

    public static void stopSendingOutSquareWave()

    {

      Marshal.Copy(new double[] { 1 }, 0, stopFlagPtr, 1);

    }

  }

 

Message Edited by arnold_w on 05-12-2009 05:46 AM
0 Kudos
Message 1 of 4
(2,267 Views)
I guess I could make a separate (console) application that calls the DLL to start the square wave. My main application would create a new process in which the console application would execute and when the square wave is no longer needed the process would be killed. This would work, but it's a bit of a hack. I would imagine there must be a more elegant solution available?
0 Kudos
Message 2 of 4
(2,244 Views)
In order to pass by reference in C# you need to use the ref keyword.
0 Kudos
Message 3 of 4
(2,233 Views)

In this case it doesn't make a difference whether I use the keyword ref or not, I have tried.

0 Kudos
Message 4 of 4
(2,231 Views)