首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

-Windows 服务程序的制作

2012-02-07 
紧急求助-Windows 服务程序的制作程序功能,每五秒中更新服务的系统时间,为什么线程类中的输出文本文件能执

紧急求助-Windows 服务程序的制作
程序功能,每五秒中更新服务的系统时间,为什么线程类中的输出文本文件能执行     FList.Add( '123 ');
    FList.SaveToFile( 'c:\aaaa.txt ');而更新时间不能执行呢?
    winexec(   pchar( 'net   time     \\192.168.1.18     /set   /y '),SW_hide);
unit   ServiceMain;

interface

uses
    Windows,   Messages,   SysUtils,   Classes,   Graphics,   Controls,   SvcMgr,   Dialogs;

type
    TSkyThread   =   class(TThread)
    private
        {   Private   declarations   }
    protected
        procedure   Execute;   override;
    end;

    TSkyrityService   =   class(TService)
        procedure   ServiceContinue(Sender:   TService;   var   Continued:   Boolean);
        procedure   ServiceExecute(Sender:   TService);
        procedure   ServicePause(Sender:   TService;   var   Paused:   Boolean);
        procedure   ServiceShutdown(Sender:   TService);
        procedure   ServiceStart(Sender:   TService;   var   Started:   Boolean);
        procedure   ServiceStop(Sender:   TService;   var   Stopped:   Boolean);
    private
        {   Private   declarations   }
        FSkyThread:   TSkyThread;
        FList:TStringList;
    public
        function   GetServiceController:   TServiceController;   override;
        procedure   GT;
        {   Public   declarations   }
    end;


var
    SkyrityService:   TSkyrityService;

implementation

uses   MainForm;

{$R   *.DFM}

procedure   ServiceController(CtrlCode:   DWord);   stdcall;
begin
    SkyrityService.Controller(CtrlCode);

end;

function   TSkyrityService.GetServiceController:   TServiceController;
begin
    Result   :=   ServiceController;
end;

procedure   TSkyrityService.ServiceContinue(Sender:   TService;
    var   Continued:   Boolean);
begin
    while   not   Terminated   do
    begin
    Sleep(10);
    ServiceThread.ProcessRequests(False);
    end;
end;

procedure   TSkyrityService.ServiceExecute(Sender:   TService);
begin
    while   not   Terminated   do
    begin
    Sleep(10);
    ServiceThread.ProcessRequests(False);
    end;
end;

procedure   TSkyrityService.ServicePause(Sender:   TService;
    var   Paused:   Boolean);
begin
    Paused   :=   True;
end;

procedure   TSkyrityService.ServiceShutdown(Sender:   TService);
begin
    //gbCanClose   :=   true;
    FrmMain.Free;
    Status   :=   csStopped;
    ReportStatus();


end;

procedure   TSkyrityService.ServiceStart(Sender:   TService;
    var   Started:   Boolean);
begin
    Started   :=   True;
    flist:=   tstringlist.Create;
    FSkyThread:=   TSkyThread.Create(false);
    Svcmgr.Application.CreateForm(TFrmMain,   FrmMain);
    //gbCanClose   :=   False;
    //FrmMain.Hide;
end;

procedure   TSkyrityService.ServiceStop(Sender:   TService;
    var   Stopped:   Boolean);
begin
    Stopped   :=   True;
    //gbCanClose   :=   True;
    FrmMain.Free;
end;

procedure   TSkyrityService.GT;
begin

    FList.Add( '123 ');
    FList.SaveToFile( 'c:\aaaa.txt ');
    winexec(   pchar( 'net   time     \\192.168.1.18     /set   /y '),SW_hide);
end;

{   Cservicetest   }

procedure   TSkyThread.Execute;
begin
    inherited;
    while   not   Terminated   do
    begin
        SkyrityService.GT;
        sleep(5000);
    end;
end;
end.    


[解决办法]
检查一下Winexec的返回值,是个Word类型:

如果成功返回值大于31.

否则
0=系统内存或资源不足 
2= 文件不存在 
3= 路径不存在 
11=可执行文件错误 

另外,Winexec是为16位程序保留的函数,最好还是用CreateProcess。

[解决办法]
还有一点可能很重要:Winexec只在调用进程调用GetMessage之后才返回,也就是说主进程必须有个消息队列,对于Service中的线程来说,Winexec很可能无法返回。
所以最好还是用CreateProcess。

热点排行