Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

stopping an async task

I have an program that collects blocks of data asynchronously. I have set the timeout to be -1 ( no timeout ).

It all works fine until I come to close the program down. The program will keep running until it gets another block of data. Is there any way of forcing the task to close?
0 Kudos
Message 1 of 5
(3,228 Views)
Hi Dan,

I hope you are doing well today! I would like to know how you are programming this. Can you post a portion of your code which highlights the issue?
Adnan Zafar
Certified LabVIEW Architect
Coleman Technologies
0 Kudos
Message 2 of 5
(3,215 Views)
Hi Adnan,

Thanks for your reply. Hopefully this code will make some sense.

I intitially remove the timeout from the task.

protected override void OnTaskCreated(EventArgs e)
{
base.OnTaskCreated(e);
Task.Stream.Timeout = -1;
}

My ReadCompleted function starts an aynchronous read for the next block of data each time it receives one.

void daqTaskComponent1_ReadCompleted(object sender, DaqTaskComponentReadCompletedEventArgs e)
{
short[,] waveforms = e.GetData();
...
// start waiting for the next frame of data.
if (!Disposing) daqTaskComponent1.ReadAsync();
}

The problem I have is when I attempt to close the program. The program will not close down because the task is still waiting for data. The only way I can stop it is to close the program normally then send it some data. I have attempted the following to hopefully shut it down but it has no effect.

protected override void Dispose(bool disposing)
{
if (disposing)
{
Task.Stop();
}

base.Dispose(disposing);
}
0 Kudos
Message 3 of 5
(3,203 Views)
Hi Dan,

Why have you removed the timeout from the task? Try this:
1) Have a timeout.
2) Try the following logic:
void OnTaskTimeout (bool disposing)
{ if (!disposing)
    Task.Start();
}

I think that should work fine. Let me know if it does or doesn't. Have a great day!
Adnan Zafar
Certified LabVIEW Architect
Coleman Technologies
0 Kudos
Message 4 of 5
(3,191 Views)
Hi Adnan,

Thanks that worked, although it has introduced me to another problem that the lack of timeout had hidden. Its related to the hardware sending the input so I have a colleague to moan at.

Thanks for your help.

Daniel Bishop
0 Kudos
Message 5 of 5
(3,184 Views)