Advice on aborting a network read with a long timeout
Show older comments
Hi all, I have a program which connects to an external instrument (via TCP/IP with the instrument control toolbox, in case it matters) to read some data. I know that this data will take a while to arrive so the timeout needs to be long. But I want the user to be able to cancel the operation via an Abort button in my GUI. I'm looking for some advice on how best to program this functionality.
As I see it, I have three options:
1. Repeated reads with short timeouts; in between each (failed) read a global abort flag is checked; timeout errors are caught and ignored. Conceptually speaking (I don't intend this code to run!):
for i=1:max_iterations
if global_abort
write_operation_cancel_command
else
try
read_with_short_timeout
catch timeout_error
end
end
update waitbar
drawnow
end
With the drawnow giving the GUI time to run the callback on the Abort button which sets the global_abort flag. This should work but it seems clumsy to me.
2. A read with a long timeout interrupted by a ctrl-c. Can you send a ctrl-c programatically? This supposes that I have a GUI with one window 'busy' in the read callback and a second with which the user can interact to send the operation_cancel_command and ctrl-c.
3. An asynchronous read with a long timeout interrupted by stopasync. Again, I'd need (somehow?!) to write a GUI with one window functioning (for the operation_cancel_command and the stopasync) and the other blocked (doing the readasync). This, I suspect, is the best option - but it's also the one about which I understand the least. Is it worth my while to look into this in detail?
Sorry for the length of this question, but I hope it's clear. Is there another, easier way that I haven't thought of? Any suggestions gratefully received! Thanks in advance.
Answers (0)
Categories
Find more on Instrument Control Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!