Author Topic: One very strange function  (Read 873 times)

0 Members and 1 Guest are viewing this topic.

Offline [SPARK] Shtorm

  • Major(1)
  • Posts: 3
One very strange function
« on: September 25, 2007, 10:35:24 am »
Hello! Can anybody explain result of this function? I write this function to show this very strange result.

Code: [Select]
procedure test_shell_ex();
var i: integer;
begin
  writeln('[SPARK] Shtorm: It is very strange, but it is fact...');
  for i:=1 to 10 do writelnfile ('testfile.txt', IntToStr(i)+' .... string'); // write to file
  shell_exec('cmd /c del /F /Q testfile.txt'); // delete file, it's file are DELETED!!!
  try   
      writeln(readfile('testfile.txt'));  // read deleted file 
  except
      writeln('none');                        // If file not exists - write 'none'
  end;
  writeln('End of function');            // bye
end;

 Then i see...

Code: [Select]
[SPARK] Shtorm: It is very strange, but it is fact...
1 .... string
2 .... string
3 .... string
4 .... string
5 .... string
6 .... string
7 .... string
8 .... string
9 .... string
10 .... string

[SPARK] Shtorm: End of function

 To get result of this function use command line "soldatserver.exe -safe 0", to work shell_exec.
 Why this function read deleted file???  :o :o :o This file can is not removed yet!!! But why???? >:(

ps: server run on WinXP.
« Last Edit: September 25, 2007, 10:42:11 am by [SPARK] Shtorm »

Offline chrisgbk

  • Moderator
  • Veteran
  • *****
  • Posts: 1739
Re: One very strange function
« Reply #1 on: September 25, 2007, 04:27:37 pm »
It's what's known as a race condition; you can't guarantee the order that things will be executed because shell exec is (eventually) an asynchronous call.

Essentially, the script finishes running before the shell command starts.

Offline EnEsCe

  • Retired Soldat Developer
  • Flamebow Warrior
  • ******
  • Posts: 3101
  • http://enesce.com/
    • [eC] Official Website
Re: One very strange function
« Reply #2 on: September 25, 2007, 06:57:46 pm »
And cmd.exe is already called in shell exec.

Offline [SPARK] Shtorm

  • Major(1)
  • Posts: 3
Re: One very strange function
« Reply #3 on: September 26, 2007, 11:38:06 am »
How can i use function, if i don't know time of perfomance???

I think, that there is a requirement to rewrite function shell_exec that it waited the ending of work shell.

And cmd.exe is already called in shell exec.

But don't work. Call "shell_exec('cmd /c del /F /Q testfile.txt');" without "cmd /c" have not any result. You may check this.
« Last Edit: September 26, 2007, 12:03:47 pm by [SPARK] Shtorm »