Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Aborting an asynchronous AI read from DAQ Assistant generated task

Used the DAQ Assistant in MS.NET (VB) to generate an asynchronous AI read with an external clock. Works fine, but want a way to terminate in the middle of the async read. Tried using StopTask but for some reason the error handler is not called so the app throws an unhandled exception when it times out. The actual task is hidden in the DAQTask.mxb. How do I abort the task from my UI?
0 Kudos
Message 1 of 6
(4,297 Views)
Hi, Steve,

The way to abort the async read is to create a public stop function in the DaqTask.User.cs so that you can access the task. The line you want to put in that function should be something like this:

            base.Task.Control(TaskAction.Abort);

Control is the method you can use to set the state of the task.

If you want more information, you can search for task.stop in the help and it talks about how you can stop the asynchronous read.

Regards,

Song D
Application Engineer
National Instrument
Regards,

Song Du
Systems Software
National Instruments R&D
0 Kudos
Message 2 of 6
(4,265 Views)

Song,

In VB.NET I added the following to DAQTask.User.vb

Protected Overrides Sub OnTaskStopping(ByVal e As System.EventArgs)

MyBase.Task.Control(TaskAction.Abort)

MyBase.OnTaskStopping(e)

End Sub

The sub is called whenever the task completes either normally or when I explicitly use DAQTaskComponent1.StopTask in my UI. I don't like calling Abort under all circumstances, but when I tried to use MyTask.Task.IsDone I get an unhandled exception if an asynchronous read is in progress.

Is the method I'm using something that NI recommends?

Steve

0 Kudos
Message 3 of 6
(4,260 Views)
Steve,

Several questions for you:

1) Can you tell us what the exception message says or post a snapshot of the exception
2) Are you using a simulated or real device? If real, what device?
3) When are you calling the IsDone property (i.e. after he aborts, after he calls the stoptask)
4) We would like some example code to look at. Even if we can't run it, we still want to see your layout.

Regards,

Song D
Application Engineer
National Instrument
Regards,

Song Du
Systems Software
National Instruments R&D
0 Kudos
Message 4 of 6
(4,251 Views)

1) I'm attaching several screen shots.

Asynch1.jpeg

This is a VS2005 debug screen shot that includes a watch on the task. All I did was call StopTask from the UI when I hit a stop button. Notice that the IsDone property shows as "error: cannot obtain value". Lots of other properties have the same error, for example the AI channel which I'm acquiring.

The ACQ is asynch generated by DAQ Assistant, not a MAX task.

In Asynch2.jpeg I show the simple method that I originally tried to use to catch if the task is done. When that code is used and I stop the task before completion using a stop button in the UI then after the task timeout period elapses I get the result shown in Asynch3.jpeg. The error code is -200284.

2) This is real hardware. PXI-1036, PXI-PCI bridge (MXI-4) and PXI-6281 DAQ

3) I'm calling IsDone from the OnTaskStopping event handler.

4) The OnTaskStopping code is included in the snap shots. Code that sets up the ACQ is below and the rest of it is auto-generated.

'Raises the TaskCreated event.

Protected Overrides Sub OnTaskCreated(ByVal e As EventArgs)

'TODO: Perform custom DAQmx task configuration, such as advanced timing and triggering, here.

'

' Refer to "Adding Custom Functionality to a .NET DAQ Component" in the NI Measurement

' Studio Help for an example of advanced DAQmx task configuration.

MyBase.OnTaskCreated(e)

'Set the ACQ timeout to 150% of the expected move time. Much safer than infinity

Task.Stream.Timeout = 1000 * 1.5 * HallGlobals.HallScanClass.NZ * HallGlobals.HallScanClass.DZ / HallGlobals.HallScanClass.ZScanSpeed

Dim ChannelLabel As String = HallRunner.MainScanForm.ComboChannels.SelectedItem

Dim ChannelName As String = HallRunner.MainScanForm.ComboVoltageSource.SelectedItem

Dim ADRange As Double = HallRunner.MainScanForm.ComboRange.SelectedItem

Dim AIScale As String = HallRunner.MainScanForm.ComboScales.SelectedItem

If AIScale Like "HPI*" Or AIScale Like "DTM*" Then

AIScale = AIScale &

"-" & HallRunner.MainScanForm.ComboHPRange.SelectedItem & "kG"

End If

'Added acq of 2nd channel. This is a temperature channel and is not user selected right now

'TODO: Make properties of 2nd channel user selectable

Task.AIChannels.CreateVoltageChannel(ChannelLabel, ChannelName, AITerminalConfiguration.Differential, -ADRange, ADRange, AIScale)

Task.AIChannels.CreateThermocoupleChannel(

"Dev1/ai0", "Thermocouple", 0, 100, AIThermocoupleType.K, AITemperatureUnits.DegreesC, 0)

Dim TaskAIChannel As NationalInstruments.DAQmx.AIChannel

TaskAIChannel = Task.AIChannels.Item(0)

TaskAIChannel.RangeHigh = ADRange

TaskAIChannel.RangeLow = -ADRange

TaskAIChannel.CustomScaleName = AIScale

TaskAIChannel.VoltageUnits = AIVoltageUnits.FromCustomScale

Task.Timing.ConfigureSampleClock("PFI1", 100000, SampleClockActiveEdge.Rising, SampleQuantityMode.FiniteSamples, HallRunner.MainScanForm.NumberOfPoints.Value)

End Sub

Steve

Download All
0 Kudos
Message 5 of 6
(4,240 Views)
Steve,

What I did was putting the following code in DaqTask.User.vb:

    Public Sub AbortAsync()
        MyBase.Task.Control(TaskAction.Abort)
    End Sub

And just associated with a button in the main form:

    Private Sub stopButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles stopButton.Click
        DaqTaskComponent1.AbortAsync()
        Button1.Enabled = True
    End Sub

This way it won't affect the normal stop. The AbortAsync() function is only called the the user specify it.

Let me know if it works.

Regards,

Song D
Regards,

Song Du
Systems Software
National Instruments R&D
0 Kudos
Message 6 of 6
(4,225 Views)