04-14-2010 11:20 AM
Hi all, I am new to coding Digital I/O cards and so I am hoping someone will be able to offer some help.
I am working in VS2005 C# with a PCI-6514 card which reads ok but I am having trouble releasing the card/ports when I try to close the application.
The reader is assigned a timeout of -1.
When I am closing the application, the following code block is called.
The messages "Try Abort", "Abort Error Handler" and "Abort Error End" are displayed, but the applicaton does not exit, it just seems to hang.
Could someone comment as to whether I am missing something from my code?
try
{
MessageBox.Show("Try Abort");Task.Control(TaskAction.Abort);}
catch (DaqException ex){
MessageBox.Show("Abort Error Handler");Task.Dispose();
MessageBox.Show("Abort Error End");}
The reader is setup using code from one of the demos but reads
private bool DigitalIO_Enable()
{
bool _Result = true;Cursor.Current = Cursors.WaitCursor;
try
{
//Create a task such that it will be disposed after
//we are done using it.
ReaderTask = new Task();//Create channel Robot Scanner
ReaderTask.DIChannels.CreateChannel(_InputPortSettings, "DGTInputChannel", ChannelLineGrouping.OneChannelForAllLines);
ReaderTask.Timing.ConfigureChangeDetection(
_InputPortSettings,
_InputPortSettings,
SampleQuantityMode.ContinuousSamples);
ReaderTask.Stream.Timeout = -1; // Infinite Timeout Setting
_DigitalReader = new DigitalSingleChannelReader(ReaderTask.Stream);
// For .NET Framework 2.0 or later, use SynchronizeCallbacks to specify that the object
// marshals callbacks across threads appropriately.
_DigitalReader.SynchronizeCallbacks = true;runningTask = ReaderTask;
_DigitalReader.BeginReadSingleSampleMultiLine(
new AsyncCallback(OnDataReady),ReaderTask);
}
catch (DaqException exception){
//MessageBox.Show(exception.Message);
_Result = false;//dispose task
if (ReaderTask != null)ReaderTask.Dispose();
}
return _Result;Cursor.Current = Cursors.Default;
}
Any help much appreciated - Shane
04-19-2010 08:23 AM
Hi Shane,
What I would reccommend is looking at the C# HSDIO examples in LabVIEW. They can be located here:
C:\Documents and Settings\All Users\Documents\National Instruments\NI-HSDIO\examples - then there should be a C# folder providing you installed support.
I hope this helps,
04-19-2010 02:17 PM
Hey Shane,
In your event handler (your async callback), do you have the check at the beginning for "if (runningTask == ar.AsyncState)"? If not, please try adding that around the entire handler block
If this does not fix the problem, please post the ex.Message string that is thrown to the message handler; perhaps this will shed some light on the behavior.
Additionally, you probably should not be aborting the task as normal flow for the application; you should be calling Task.Stop() unless something's wrong. I would recommend looking at the DAQmx examples to see what you should be doing located in C:\Documents and Settings\All Users\Documents\National Instruments (or C:\Users\Public\Documents\National Instruments, if Vista/Win7) and drill down under DAQmx (not HSDIO) and find the C# digital examples.
04-21-2010 09:17 AM
Guys.
Thanks for the replies.
I have run some simplified tests on the target machine which is Windows 7 in case that makes a difference.
There are now 2 buttons, 1 which enables the _readerTasks etc. and 1 which disables it, the code for each method is below.
If I run the enable, it executes without error. If I then run the disable, the system locks up and does not return; no errors are detected.
private bool DigitalIO_Enable()
{
bool _Result = true;
try
{
Cursor.Current = Cursors.WaitCursor;// Read Task Settings
_readerTask = new Task(); //Create a task such that it will be disposed after we are done using it.
_readerTask.DIChannels.CreateChannel(_InputPortSettings, "DGTInputChannel",ChannelLineGrouping.OneChannelForAllLines); //Create channel Robot Scanner
_readerTask.Timing.ConfigureChangeDetection(_InputPortSettings,
_InputPortSettings,
SampleQuantityMode.ContinuousSamples);_readerTask.Stream.Timeout = -1; // Infinite Timeout Setting
_runningTask = _readerTask;
// _DigitalReader Settings
_DigitalReader = new DigitalSingleChannelReader(_readerTask.Stream);_DigitalReader.SynchronizeCallbacks = true; // For .NET Framework 2.0 or later, use SynchronizeCallbacks to specify that the object marshals callbacks across threads appropriately.
_DigitalReader.BeginReadSingleSampleMultiLine(new AsyncCallback(OnDataReady),_readerTask);
}
catch (DaqException exception){
_Result = false;//dispose task
if (_readerTask != null)_readerTask.Dispose();
}
finally
{
Cursor.Current = Cursors.Default;}
return _Result;
}
--------------------------------------------------------------------------------------------
private void DigitalIO_Disable()
{
try
{
_runningTask = null;_readerTask.Dispose();
}
catch (Exception ex){
MessageBox.Show(ex.ToString()); throw;}
}
04-21-2010 02:13 PM
I'm assuming this is not your entire program since I don't see a definition of the OnDataReady callback?
Did you try if (runningTask == ar.AsyncState) check inside of the callback? Have you tried putting a breakpoint or message box inside of the catch block to make sure that you're not missing an error: _Result = false;
//dispose task
if (_readerTask != null)_readerTask.Dispose();
06-21-2010 10:55 AM
Other tasks have taken focus away from this but now we are back to trying to resovle it to make the application stable.
It would appear that the issue may be to do with the timout being set to -1.
When I use the sample app ReadDigChan_ChangeDetection, I can reproduce the issue by setting the timeout to -1;
Task.Stream.Timeout = -1
Is it possible that this leaves the code with no entry point to execute the code to stop and this means the resources are not released?
This would explain why on reentry the app says the resouces are reserved.
If I remove the infinite time out, then I can execute the reset as per the sample application.
This is very similar to the implementation I have in my application.
As an alternative, is there a way to interrupt or reset the timeout to allow this to execute?
Thanks again
Shane