PDA

View Full Version : Problem with file.find and cancel button



Solmos
11-04-2007, 01:41 PM
Hi

I needed one function for search files with cancel button (StatusDlg)

for example:


StatusDlg.Show()

r = File.Find(Drive.."", "*.exe", false, false,nil, nil);
error = Application.GetLastError();
if error ~= 0 then
else
if (r == nil) then
else

for index, path in r do

x = StatusDlg.IsCancelled();
if x == true then
StatusDlg.Hide();
Application.ExitScript();
end

StatusDlg.SetTitle("xxx");
pn = String.SplitPath(path);
rr = pn.Filename..""..pn.Extension;

StatusDlg.ShowProgressMeter(false);
StatusDlg.SetMessage("");

StatusDlg.SetStatusText(rr);

--------others actions here...

end
end
end


StatusDlg.Hide()

if press the button cancel, function is not stop!!!???

:huh:huh:huh:huh

usernameCasper
11-05-2007, 07:59 AM
Hey Solmos,

I think you need a reorganisation of your syntax.
Some actions are redundant that causes more CPU usage.

Why dont you use:


if (StatusDlg.IsCancelled() == false) then
pseudo-code: do not continue
break;
else
pseudo-code: continue
perform code execution
end


Try to use the 'break' keyword, this will break out loops.
Another way to overcome this issue is using a custom callbackfunction.
DO NOT use 'Application.ExitScript()' while you performing loop operations !
E.g. statusdialog is still visible (like other elements) that aren't "free'ed" yet.
They need to be released, and this is done by the 'break'-keyword or callbackfunction.

Try to structure your code:


StatusDlg.Show(64,false);
StatusDlg.SetTitle("xxx");
StatusDlg.SetMessage("");
StatusDlg.SetStatusText("Searching");
StatusDlg.ShowProgressMeter(false);
StatusDlg.ShowCancelButton(true,"Cancel");

local r = File.Find("D:\\", "*.exe", true, true,nil, nil);
if (r ~= nil) then
for nrIndex, strPath in r do
local x = StatusDlg.IsCancelled();
if (x == true) then -- OR: if x then
StatusDlg.Hide();
break;
else
local pn = String.SplitPath(strPath);
local rr = pn.Filename..""..pn.Extension;
--------others actions here...
end
end
StatusDlg.Hide();
end


Hope this helps,
Casper

NOTE: Some alligns are changed during copy-paste