system file watch crash
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Guillaume
2015-1-26
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Call Java from MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!