如果一个主进程可以创建多个子进程。如何实现主进程与子进程之间的通讯,而且主进程完成与子进程通讯完后,主进程可以关闭子进程。
   愿听高论!

解决方案 »

  1.   

    加一个全局进程变量..
    至于关闭某一个进程
    terminateThread
    直接杀死它..
      

  2.   

    要交换大块数据时用CreateFileMapping(),MapViewOfFile()创建共享内存,
    小数据可用消息,自定义一个消息,以广播的方式发送就可以了,
    如果使用共享内存,可能还需要使用TEvent来进行同步。一小段程序:
    ======
    呼叫方:
    function CreateMapFile(var AMappingHandle:THandle;var AMemory:Pointer;ADesiredSize:DWORD):Boolean;
    begin
      Result:=false;
      AMappingHandle:=CreateFileMapping($FFFFFFFF,nil,PAGE_READWRITE,0,ADesiredSize,ProgramGUID);
      if AMappingHandle=0 then
        exit;
      AMemory:=MapViewOfFile(AMappingHandle,FILE_MAP_WRITE,0,0,0);
      if AMemory=nil then
        exit;
      Result:=true;
    end;function ReleaseMapFile(var AMappingHandle:THandle;var AMemory:Pointer):Boolean;
    begin
      Result:=false;
      if AMemory<>nil then begin
        if not UnmapViewOfFile(AMemory) then
          exit;
        AMemory:=nil;
      end;
      if AMappingHandle<>0 then begin
        if not CloseHandle(AMappingHandle) then
          exit;
        AMappingHandle:=0;
      end;
      Result:=true;
    end;procedure ShowPhotoEnhanceForm(AOwner:TComponent;AFileName:String);
    var
      MemStream:TMemoryStream;
      MappingHandle:THandle;
      Memory:Pointer;
      Event:TEvent;
    begin
      MemStream:=TMemoryStream.Create();
      try
        if not GetImageStream(AFileName,MemStream) then begin
          raise Exception.Create('ShowPhtotEnhanceForm: error when get image stream!');
        end;
        if not CreateMapFile(MappingHandle,Memory,MemStream.Size) then begin
          raise Exception.Create('ShowPhotoEnhanceForm: error when create map file!');
        end;
        try
          Event:=TEvent.Create(nil,true,false,EventGUID);
          Event.ResetEvent();
          try
            MemStream.Position:=0;
            MemStream.ReadBuffer(Memory^,MemStream.Size);
            if ShellExecute(Application.Handle,'open',pChar(GetPEPathName()),pChar('/m'+IntToStr(MemStream.Size)),'',SW_NORMAL)<=32 then begin
              raise Exception.Create('ShowPhotoEnhanceForm: error to execute photo enhance!');
            end;
            case Event.WaitFor(WaitTimeout) of
              //wrSignaled:
              wrTimeout:  raise Exception.Create('ShowPhotoEnhanceForm: The time specified by the TimeOut parameter elapsed without the signal being set.');
              wrAbandoned:raise Exception.Create('ShowPhotoEnhanceForm: The event object was destroyed before the TimeOut period elapsed.');
              wrError:    raise Exception.Create('ShowPhotoEnhanceForm: An error occurred while waiting.');
            end;
          finally
            Event.Free;
          end;
        finally
          if not ReleaseMapFile(MappingHandle,Memory) then begin
            raise Exception.Create('ShowPhotoEnhanceForm: error when release map file!');
          end;
        end;
      finally
        MemStream.Free;
      end;
    end;
    接收方:
    procedure TMainForm.GetMemoryBitmap(AMemSize:DWORD;var ABitmap:TBitmap);
    const
      ProgramGUID='{E95E2974-C6A3-45BA-AC0A-876FCF9523FD}';
      EventGUID='{56717769-5DD0-4B20-830A-43A6AB753E1F}';
    var
      MappingHandle:THandle;
      Memory:Pointer;
      MemStream:TMemoryStream;
      Event:TEvent;
    begin
      try
        MemStream:=TMemoryStream.Create();
        try
          MappingHandle:=CreateFileMapping($FFFFFFFF,nil,PAGE_READWRITE,0,AMemSize,ProgramGUID);
          if MappingHandle=0 then
            raise Exception.Create('GetMemoryBitmap: error when create mapping file!');
          Memory:=MapViewOfFile(MappingHandle,FILE_MAP_WRITE,0,0,0);
          if Memory=nil then
            raise Exception.Create('GetMemoryBitmap: error when map view of file!');
          MemStream.Size:=AMemSize;
          MemStream.Position:=0;
          MemStream.WriteBuffer(Memory^,AMemSize);
          MemStream.Position:=0;
          ABitmap.LoadFromStream(MemStream);
        finally
          if Memory<>nil then
            UnmapViewOfFile(Memory);
          if MappingHandle<>0 then
            CloseHandle(MappingHandle);
          MemStream.Free;
        end;
      finally
        Event:=TEvent.Create(nil,true,false,EventGUID);
        try
          Event.SetEvent();
        finally
          Event.Free;
        end;
      end;
    end;
      

  3.   

    进程间通讯有这么两个办法:
    1、全局变量通讯,
    2、进程间发送消息;
    发送消息的话比较复杂,一般用全局变量比较简单些,但是注意不要引起冲突!
    另外我觉得你最好去
    http://net-edu.hdpu.edu.cn/teach/network/DELPHI%205%E5%BC%80%E5%8F%91%E4%BA%BA%E5%91%98%E6%8C%87%E5%8D%97/
    将《程序员开发指南》一书下载下来,比较好,讲的进程很详细~~~~
    强烈推荐!!!!!!!!!!!!!!
      

  4.   

    干吗不用Event,Delphi7有个TShellChangeNotifier组件,你读一读它的源码,就可以知道了。你知道线程池的实现方法和原理吗?主线程创建N个线程,然后循环利用它们。这其中就涉及到主线程是如何跟子线程通信的问题。结合如下内容阅读
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/using_event_objects.asp
      

  5.   

    这个问题,我已经解决了。用了最简单的方法,那就是用GloabAlloc的共享内存的方法。也是最简便,最有效的方法 结贴了!