为什么我的应用程序在从Windows Scheduler运行时会冻结?

时间:2013-03-10 11:28:53

标签: delphi

我有一个简单的应用程序:

uses
  SysUtils;

{$R *.res}

procedure Log(text: string);
var
  myFile: textfile;
begin
  AssignFile(myFile, 'my.log');
  if not(FileExists('my.log')) then
    Rewrite(myFile)
  else
    Append(myFile);
  Writeln(myFile, text);
  CloseFile(myFile);
end;

begin
    Log(TimeToStr(Now)+' Passed!');
end.

当我尝试通过Windows任务计划程序启动此应用程序时,我遇到了问题。   计划任务的状态为“正在运行”,但没有任何反应。

1 个答案:

答案 0 :(得分:2)

您需要提供保存文件的完整路径。当Windows任务计划程序启动您的应用程序时,应用程序的工作目录将位于任务计划程序应用程序所在的任何位置。反过来,Windows不允许您的应用程序在该位置保存文件。因此,您必须确切地告诉它保存文件的位置。

procedure Log(text: string);
var
  myFile: textfile;
  Filename: String;
begin
  Filename:= IncludeTrailingBackslash(ExtractFilePath(ParamStr(0)))+'my.log';
  AssignFile(myFile, Filename);
  if not(FileExists(Filename)) then
    Rewrite(myFile)
  else
    Append(myFile);
  Writeln(myFile, text);
  CloseFile(myFile);
end;

我假设Windows因为你的标题“冻结”而拒绝访问。尽管如此,即使问题未被拒绝,您仍然不知道这个位置在哪里。