system file watch crash
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
AA
el 26 de En. de 2015
Respondida: Guillaume
el 26 de En. de 2015
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 comentarios
Respuesta aceptada
Guillaume
el 26 de En. de 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 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Call Java from MATLAB en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!