system file watch crash
2 views (last 30 days)
Show older comments
fsw = System.IO.FileSystemWatcher();
fsw.Path = 'C:\Users\wolfgang\Desktop';
fsw.Filter = 'NZDUSD1.csv';
fsw.EnableRaisingEvents = true;
listenerhandle = addlistener(fsw, 'Changed', @(~,~)importfcn(q,a));
%signature of importfcn is function importfcn(sender, eventargs)
%add a small delay in importfcn before reading the file as the event is raised
%to make sure that file modification is complete
When I run this file, .csv file gets imported every minute and then processed. Sometimes, the imported file is still processed and the next import is delayed. This leads to the delay of several files.
In the end my matlab crashed and I get an error message. How can I resolve this problem?
2 Comments
Accepted Answer
Guillaume
on 26 Jan 2015
Where do the q and a come from in your anonymous function?
As there is no point detecting file modifications while you're already processing them, I would disable the FileSystemWatcher at the beginning of importfcn and reenable it at the end. You could also check for modification right at the end of the function and process again if it has. Something like:
function importfcn(sender, eventargs)
sender.EnableRaisingEvents = false; %disable filesystemwatcher
%wait a little while to make sure modifications are complete
fpath = fullfike(sender.Path, sender.Filter);
oldmtime = System.IO.File.GetLastWriteTime(fpath);
while true
%process file
newmtime = System.IO.File.GetLastWriteTime(fpath);
if oldmtime.Equals(newmtime)
break %file hasn't changed. break out of loop
end
end
sender.EnableRaisingEvents = true;
end
warning: completely untested code. May contain bugs.
0 Comments
More Answers (0)
See Also
Categories
Find more on Call Java from MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!