Delphi service always runs at 15% processor load -
Delphi service always runs at 15% processor load -
i encountered problem service wrote time ago. service meant translate various programs calling multilizer application via createprocess
. problem 1 time checks of given directories cleared , actual translation method called cpu load rising 15-20% , remain @ level if files have been processed. onstart-event of service creates non delayed thread has execute-method looks this:
procedure ttranslatethread.execute; var count : integer; begin sleep(serviceobject.hinterval); while not terminated begin inc(count); if count>= 10 begin count :=0; if serviceobject.checkdircount>0 begin serviceobject.translateservice; sleep(serviceobject.hinterval); end; end; end;
however suppose main cause of problem lies in way have phone call multilizer. because service has wait multilizer finish translating. used waitforsingleobject
wait multilizer finish although know it's kind of bad idea. method calls multilizer:
procedure waitforml7(hname: string); var si: tstartupinfo; pi: tprocessinformation; hcreateok: boolean; aparameterfinal,afilename: string; begin afilename := hmultilizerpath+'ml7build.exe'; aparameterfinal := 'b '+hname+'.exe.m7p'; fillchar(si,sizeof(tstartupinfo),#0); fillchar(pi,sizeof(tprocessinformation),#0); si.cb := sizeof(tstartupinfo); aparameterfinal := format ('"%s" %s', [afilename, trimright(aparameterfinal)]); slog.info('createprocess wird versucht'); hcreateok := createprocess(nil,pchar(aparameterfinal), nil, nil, false, create_new_console or normal_priority_class, nil, pchar(hmultilizerpath) ,si,pi); if hcreateok begin slog.error('multilizeraufruf war erfolgreich für prg: '+hname); waitforsingleobject(pi.hprocess,infinite); end else begin slog.error('aufruf war nicht erfolgreich -> keine uebersetzung'); end; closehandle(pi.hprocess); closehandle(pi.hthread); end;
i don't understand why processor load remains high there nil more do. in advance.
your thread runs busy loop.
while not terminated begin inc(count); if count>= 10 begin count :=0; if serviceobject.checkdircount>0 begin serviceobject.translateservice; sleep(serviceobject.hinterval); end; end; end;
note i've added missing end
code. hope that's error made, because when post code not real code, it's plausible problem lies in real code rather code posted.
anyway, suppose checkdircount
evaluates 0
, loop looks this:
while not terminated begin inc(count); if count>= 10 begin count :=0; serviceobject.checkdircount; end; end;
that busy loop. when run busy loop, processor gets hot.
i don't want propose solutions because don't know of details of programme doing. i'm trying reply question of why thread consumes cpu.
delphi windows-services delphi-xe3
Comments
Post a Comment