Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

c# event not firing during daqmxTask.Read and daqmxTask.write

My program is written in visual C# and NI-DAQMX.  I have a routine that writes an analog output then reads the corresponding analog input (in a loop).  I have a button that stops the data acquisition (assigns a variable to true) and exits the routine.  The stop button doesn't work.  It looks like window sees the event (the interface freezes NOT the program) but doesn't act on it.  The data acquisition keeps going ignoring the event.  I've tried putting the read in a separate thread.  I've tried monitoring the event in a separate thread.  Nothing works. 
 
Any help will be appreciated!  Thanks.
0 Kudos
Message 1 of 6
(4,512 Views)

Hi karenho,

Its not clear from your post exactly what the problem you are having is so allow me to suggest and ask a few things.

1) Have you taken a look at our synchronization examples in the following directories:
    Windows Vista -
C:\Users\Public\Documents\National Instruments\NI-DAQ\Examples\DotNET2.0\Synchronization
    Windows XP - C:\
Documents and Settings\All Users\Documents\National Instruments\NI-DAQ\Examples\DotNET2.0\Synchronization

These example should give you a good starting point

2) When you click the stop button, what code does the stop button actually perform?
    Are you calling Task.Stop or Task.Dispose?
    What task does it stop? The AO task or AI task?

3) When you click the stop button, you say that the UI is non-responsive but you are still reading data?

4) Does this issue occur both in release or debug mode?

I don't want to start guessing to much as your problem is still unclear to me. A small example would always help.

Best Regards,

Jonathan N.
National Instruments
0 Kudos
Message 2 of 6
(4,507 Views)

Here is an example of what I'm trying to do.  When you activate the stop button, it should set bStopTest to true.  Then, when function() gets to the point where it checks on bStopTest, function() should abort the tasks and exit.  Instead, it keeps going and ignores that bStopTest is set -- meaning bStopTest is never set to true.  The UI also freezes at this point until function() is done.  The program doesn't freeze just the UI.

Even if you were to activate the stop button before the program goes into function(), bStopTest is false.  But if I put this "bStopTest = true" right before "if(bStopTest == true)" in function(), it'll abort the tasks.

I don't think this is a synchronization problem.  This is in debug mode, did not try release mode.

==============================

private void stopTest_Click(object sender, System.EventArgs e)  // stop button

{

bStopTest = true;

}

public void function()

{

Task2.Start();  // analog inputs

Task2Writer.WriteSingleSample(true, Data[0]);

for(...)

{

Task2.WriteSingleSample(true, Data[j]);

Thread.Sleep(100);

Reader.ReadSingleSample();

if(bStopTest == true)  // it never gets to this point

{

Task1.Control(TaskAction.Abort);

Task2.Control(TaskAction.Abort);

return;

}

// do something....

}

}

0 Kudos
Message 3 of 6
(4,482 Views)
Hi karenho,

The problem that you are seeing doesn't have anything to do with DAQ but rather just general programming structure.  The way you have your application set up now doesn't allow for UI messages to be processed at the same time you are acquiring data.  Essentiall you need to free up the UI to process events while you are in the for loop. Your current structure has you locked in that for loop and not allowing for UI messages to be processed. Thus your button click event shouldn't be fired until the for loop finishes.  So the recommended approach in DAQ to what you are wanting to do is to use our asynchronous model.  You can read about this model in the Asynchronously Reading and Writing with the NI-DAQmx .NET Class Library help topic found in the NI Measurement Studio Help. You can also see how the model is implemented by reviewing any of our continuous acquisition examples.

By following this model, you will free up the UI to process messages at the same time your are reading data. 

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

so does something like this work because it doesn't work for me.  i got the same result.  thanks.

====================

double[] data = new double[4];

double[] readData = new double[4];

private void stopTest_Click(object sender, System.EventArgs e)  // stop button

{

bStopTest = true;

}

public void function()

{

Task2.Start();  // analog inputs

Task2Writer.WriteSingleSample(true, Data[0]);

Task1Reader.BeginReadSingleSample(new AsyncCallback (callBackFunc), null);

for(...)

{

Task2.WriteSingleSample(true, Data[j]);

Thread.Sleep(100);

data = readData;

if(bStopTest == true)  // it never gets to this point

{

Task1.Control(TaskAction.Abort);

Task2.Control(TaskAction.Abort);

return;

}

// if not bStopTest do something....

}

}

public void callBackFunc(IAsyncResult i)

{

readData = Task1Reader.EndReadSingleSample(i);

Task1Reader.BeginReadSingleSample(new AsyncCallback (callBackFunc), null);

}

0 Kudos
Message 5 of 6
(4,454 Views)
Hi karenho,

You are still not allowing your UI to process events by using your for loop. You need to remove your for loop and just use callbacks for your reads and writes. The example that I would suggest you look at is almost perfect for you is SyncAIAO_DigStart found in the Synchronization\Multi-Function folders in the directories I listed earlier. That example demonstrates how to how to continuously acquire and generate synchronized analog input and output, started by an external digital start trigger.  Now you don't have to use a digital start trigger, but you would want to choose some trigger to sync up your AI and AO tasks.  Also, in that example the writer isn't doing asyn writes but you could change that and set up a  callback just like the reader is doing.

That example also shows you how to properly start and stop your tasks with the start and stop buttons. Look at the code behind them.

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