From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

Multifunction DAQ

cancel
Showing results for 
Search instead for 
Did you mean: 

waiting for task to complete

Solved!
Go to solution

I inherited a program that intensively tests a hardware device.  One of the tests turns each AI channel on and off multiple times and reads the waveform of the AI channel.  I am using a DAQ PXI 6229 card with C# .NET.  The program is hanging in the while loop waiting for the task to complete. This is the code:

 

public bool TestThisChannel(object nullObject) {
            double coversionFactor;
            string Name;
            string Description;
            _PXI6229_SLOT12.getChannelInfo((int)nullObject,out  Name,out  coversionFactor,out Description);
            _PXI6229_SLOT12.StartAnalogInput6s(Name);
            for (int i = 0; i < 5; i++) { //5 times
                _PXIChassis.SetPowerRelayOn(null);
                _PXIChassis.Wait500ms(null);
                _PXIChassis.SetPowerRelayOff(null);
                _PXIChassis.Wait500ms(null);
            }
           
            while (_PXI6229_SLOT12.runningTask!=null) {
            } //Wait for task to complete

 private Task AnalogMultipleInputTask;
        public Task runningTask;
        private AsyncCallback analogCallback;
        private AnalogMultiChannelReader analogInReader;
        public AnalogWaveform<double>[] data;

        public void StartAnalogInput6s(string AIChannel)
        {
             // Create a new task
            try {
                AnalogMultipleInputTask = new Task();

                // Create a virtual channel
                AnalogMultipleInputTask.AIChannels.CreateVoltageChannel(AIChannel, "",
                    (AITerminalConfiguration)(-1), -10 , 10, AIVoltageUnits.Volts);

                // Configure the timing parameters
               
                AnalogMultipleInputTask.Timing.ConfigureSampleClock("", 100000, SampleClockActiveEdge.Rising, SampleQuantityMode.ContinuousSamples, 600000);
               
                // Verify the Task
                AnalogMultipleInputTask.Control(TaskAction.Verify);

                // Prepare the table for Data
                runningTask = AnalogMultipleInputTask;
                analogInReader = new AnalogMultiChannelReader(AnalogMultipleInputTask.Stream);
                analogCallback = new AsyncCallback(EndAnalogInput);

                // For .NET Framework 2.0 or later, use SynchronizeCallbacks to specify that the object
                // marshals callbacks across threads appropriately.
                analogInReader.SynchronizeCallbacks = true;
                analogInReader.BeginReadWaveform(600000, analogCallback, AnalogMultipleInputTask);
            } catch (Exception exception) {
                MessageBox.Show(exception.Message);
                runningTask = null;
                AnalogMultipleInputTask.Dispose();
            }
        }

 public void EndAnalogInput(IAsyncResult ar)
        {
            try
            {
                 if (runningTask == ar.AsyncState)
                {
                    // Read the available data from the channels
                    data = analogInReader.EndReadWaveform(ar);
  }
     }
     catch(DaqException exception)
     {
  //Display Errors
  MessageBox.Show(exception.Message);
  runningTask = null;

AnalogMultipleInputTask.Dispose();
     }
 }

 

 

Any help will be appreciated.

 

Thanks,

Howard

0 Kudos
Message 1 of 10
(4,100 Views)

Hello Howard,

 

You said that you inherited this code--did it work for the previous owner?  Has it worked for you at all?

 

 

Mason M
Applications Engineer
National Instruments
0 Kudos
Message 2 of 10
(4,073 Views)

Hi Mason,

       Some of the code is working.  This code appears to be working in the sense that there are no error messages being produced.  In order to test where it is hanging I have previously placed a print statement in the while loop that is waiting for the task to end and it is definitely in an infinite loop waiting for this to happen.  Is this the correct method to wait for the end of the task?  I am a novice as far as this type of programming goes.

 

Thanks,

Howard

0 Kudos
Message 3 of 10
(4,068 Views)

Howard,

 

Where is the object  _PXI6229_SLOT12 instantiated?  When was this code written?  I would recommend looking at the NI examples for DAQmx programming in C#.  These are located in Start » All Programs » National Instruments » NI-DAQ » Text-Based Code Support.  You might try to see if you can get one of the examples to run with your hardware to make sure you are communicating with it properly.

 

Mason M
Applications Engineer
National Instruments
0 Kudos
Message 4 of 10
(4,048 Views)

There is no problem with communication.  This is just one of many tests that are performed on the device.  The previous tests I have run are producing correct results.  The problem I am having with this particular section of code is that the task never seems to end.  The while  (while (_PXI6229_SLOT12.runningTask!=null) {  })  is in an infinite loop.  Is there a better way to handle checking for completion of the task?

0 Kudos
Message 5 of 10
(4,037 Views)

I have also tried the following: while (!_PXI6229_SLOT12.runningTask.IsDone) { }  and _PXI6229_SLOT12.runningTask.WaitUntilDone() with the same results.

0 Kudos
Message 6 of 10
(4,027 Views)

Something else I have found is that the callback is not being executed.  What would stop the callback from being executed?

0 Kudos
Message 7 of 10
(4,022 Views)

You're waiting for runningTask to become null, but in your "StartAnalogInput6s" function the only place that runningTask is assigned null is in the try catch.  Therefore the task will not stop running.  

 

you need some logic like this:

 

while (_PXI6229_SLOT12.runningTask!=null && !complete)

 

where "complete" is a boolean that gets set to TRUE at the end of "StartAnalogInput6s".  This way if you hit an exception (runningTask==null) OR if the task finishes you will exit the loop.  

Mason M
Applications Engineer
National Instruments
0 Kudos
Message 8 of 10
(3,975 Views)
Solution
Accepted by topic author paofthree

Thanks Mason. You got me thinking on the right path.  I made some changes based on an example I found from NI.  I changed the analogInReader.BeginReadWaveform(..........) to data = analogInReader.ReadWaveform(600000) and I changed the while (_PXI6229_SLOT12.runningTask!=null) { } to _PXI6229_SLOT12.runningTask.WaitUntilDone() and this appears to be working correctly. 

 

Thanks for all your help.

 

Howard

0 Kudos
Message 9 of 10
(3,963 Views)

No problem, glad I could help out!

Mason M
Applications Engineer
National Instruments
0 Kudos
Message 10 of 10
(3,958 Views)