Multifunction DAQ

cancel
Showing results for 
Search instead for 
Did you mean: 

Task Stop occasionally takes a long time

Every once in a while, when we call Stop on a DAQmx task, it takes about 30 seconds to stop.  I've searched the forum and can only find cases where this sort of thing happens consistently.  Any suggestions as to what could cause this to happen intermittently?

 

Thanks

0 Kudos
Message 1 of 5
(2,693 Views)

Can you provide a bit more details. How do you "call stop" (programming language, etc.) and what's the task doing at that time?

0 Kudos
Message 2 of 5
(2,443 Views)

Sorry for the lack of detail.  Language is C# in Visual Studio.  Below is a code snippett of how I initialize the task:

 

myTask = new NationalInstruments.DAQmx.Task("MyReadTask");

physicalChannels = DaqSystem.Local.GetPhysicalChannels(PhysicalChannelTypes.All, PhysicalChannelAccess.All);

if (physicalChannels.Count() > 1)
   pressureChannel = physicalChannels.FirstOrDefault(p => p.EndsWith("/ai" + millarPressureChannel.ToString()));

myTask.AIChannels.CreateVoltageChannel(MillarPressureChannel,
"MillarChannel", AITerminalConfiguration.Differential, -5, 5, AIVoltageUnits.Volts);


myTask.Timing.SamplesPerChannel = (int)samplingRate * 4;
myTask.Timing.SampleClockSource = "Internal";
myTask.Timing.ConfigureSampleClock("", samplingRate, SampleClockActiveEdge.Rising, SampleQuantityMode.ContinuousSamples);
myTask.Timing.SampleClockRate = samplingRate;

 

myTask.Control(TaskAction.Verify);
PressureChannelReader = new AnalogSingleChannelReader(myTask.Stream);
myTask.EveryNSamplesReadEventInterval = 50;
myTask.EveryNSamplesRead += new EveryNSamplesReadEventHandler(Every50SamplesRead);
System.Threading.Tasks.Task.Run(() => myTask.Start());

 

Here is my Read even handler:

 

private void Every50SamplesRead(object sender, NationalInstruments.DAQmx.EveryNSamplesReadEventArgs e)
{
   PressureChannelReader.BeginReadMultiSample(-1, Pressure50SamplesCallback, null);

}

 

Here is my callback function:

 

private void Pressure50SamplesCallback(IAsyncResult ar)
{

   try
   {

      double[] dataBlock = PressureChannelReader.EndReadMultiSample(ar);

      // Do stuff with dataBlock

   }
   catch (DaqException exception)
   {

      // Handle exception

   }

}

 

Here is what I do when I stop data collection:

 

System.Threading.Tasks.Task.Run(() => StopDAQTask());

 

private void StopDAQTask()
{

   if (myTask != null)
   {
      // We found that if you're displaying data and unplug the DAQ USB cord then when
      // Stop() gets called it throws an exception here. So just catch it and continue on.
      try
      {
         myTask.Stop();
      }
      catch (Exception ex)
      {
         // Log exception
      }
      try
      {
         myTask.Dispose();
      }
      catch (Exception ex)
      {
         // Log exception
      }
      myTask = null;
   }
}

 

Thanks for taking a look

0 Kudos
Message 3 of 5
(2,278 Views)

I've changed my application a bit to try to narrow down what is going wrong.  I made it so that it uses the code I posted above to start collecting data, then after 10 seconds it stops collecting data (also using the code posted above), then it sleeps for 2 seconds and starts over again.  On the 332nd iteration the call to Task.Stop() took over 17 seconds to complete, then the application simply stopped (which is strange because I have an exception handler in the application that should have caught any exceptions thrown).  However, right at that time the following information was in the Windows event log.  Does this give anyone a clue as to what could be going wrong?  My next step is to create a completely separate application that does ONLY what I described above, eliminating any other overhead, libraries, etc. that the rest of my application could be adding.

 

Framework Version: v4.0.30319 Description: The process was terminated due to an unhandled exception. Exception Info: System.AccessViolationException at <Module>.nNIMSAI100.calculateNumSamples(nNIMSAI100.tTask*, Int32*, nNIMDBG100.tStatus2*)

at <Module>.nNINETAI2005100.Read1DHelper<class nNIMSSAIL100::ApiTraits<class nNIMSSAIL100::DotNetApi>,class nNIMSSAIL100::AnalogRead,double>(NationalInstruments.DAQmx.Task, nNIMSSAIL100.tNumChannels, Int32, nNIMSSAIL100.tNumLines, Double, Double[] ByRef, NationalInstruments.DAQmx.ReallocationPolicy, Int32 ByRef, Boolean)

at NationalInstruments.DAQmx.Internal.AnalogSingleChannelReaderImpl.ReadMultiSample(Int32)

at NationalInstruments.DAQmx.Internal.AnalogSingleChannelMultiSampleReadAsyncResult.SampleBasedReadWorkItem(System.Object)

at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(System.Object)

at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)

at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()

at System.Threading.ThreadPoolWorkQueue.Dispatch()

at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()

0 Kudos
Message 4 of 5
(1,876 Views)

I've continued to experiment with this.  While doing so I noticed that I sometimes get exceptions in my Pressure 50SamplesCallback function.  It seems to happen if the task gets stopped right at the same time that the callback function is called.  Could the problem I noted above somehow be related to something similar (like me trying to read and stop at the same time in different threads)?

 

Thoughts?

 

Thank you

0 Kudos
Message 5 of 5
(1,858 Views)