Newsgroups : Borland : borland.public.delphi.internet.winsock : 2007 Dec : Re: closing an application over the network

www.cryer.info
Managed Newsgroup Archive

Re: closing an application over the network

Subject:Re: closing an application over the network
Posted by:"soonic" (xxsoon..@op.pl)
Date:Fri, 21 Dec 2007 22:05:20

>
> Notice in my earlier example that I use PostThreadMessage() instead of
> PostMessage().  Did you try PostThreadMessage() yet?

I changed for what you said (but it changed nothing) and message handler
looks:

const   WM_MYMSG = WM_USER + 1000;

procedure TMainForm.CTIStartup(Sender: TObject; var ShowMainForm: Boolean);
begin
  PostThreadMessage(MainThreadID, WM_MYMSG,0,0);
  ShowMainForm := False;
end;

procedure TMainForm.MyMsg(var Msg: TMsg; var Handled: Boolean);
begin
  if Msg.message = WM_MYMSG then
  begin
    Handled := True;
    FormShow(nil);
  end;
end;

This is how it's set the PostThreadMessage().


> Then clearly you are not setting everything up correctly at program
> startup when using the tray icon.

Probably you're right at here.
So, when it calls my FormShow, it look as fallows:
(real code)

procedure TMainForm.FormShow(Sender: TObject);
var Dir: String;
    F: File;
begin
  if CTI.Tag = 0 then
  begin
    AddBtn.Enabled := False;
    DelBtn.Enabled := False;
    SaveBtn.Enabled := False;
    try
      Dir := ExtractFilePath(Application.ExeName);
      AssignFile(F, Dir+FileName);
      Reset(F,1);
      with OptionForm do
      begin
        ...
        BlockRead(F, pCB2st, SizeOf(pCB2st));  //reads the state for
checkbox
        BlockRead(F, IPnum1, SizeOf(IPnum1));
        BlockRead(F, IPnum2, SizeOf(IPnum2));
        BlockRead(F, IPnum3, SizeOf(IPnum3));
        BlockRead(F, IPnum4, SizeOf(IPnum4));
        BlockRead(F, Port, SizeOf(Port));

        pCB2.Checked := pCB2st;
        nIP1.Text := IntToStr(IPnum1);
        nIP2.Text := IntToStr(IPnum2);
        nIP3.Text := IntToStr(IPnum3);
        nIP4.Text := IntToStr(IPnum4);
        IP := IntToStr(IPnum1) +'.'+ IntToStr(IPnum2) +'.'+ IntToStr(IPnum3)
+'.'+ IntToStr(IPnum4);
        nME2.Text := IntToStr(Port);
        if  pCB2st = True then ConBtnClick(nil);   //  <-----------------
      end;
      CloseFile(F);
    except
        Application.MessageBox('.....');
        OptionForm.OKBtnClick(MainForm); //loads defaults
    end;
    CTI.Tag := 1;
  end;
end;

//Connect button for user, when startup autoconnect disabled pCB2st = False
procedure TMainForm.ConBtnClick(Sender: TObject);
begin
  try
    ConBtn.Enabled := False;
    NetUnit.ConnectServer;
  except
    ConBtn.Enabled := True;;
  end;
end;

//in netunit.pas
procedure ConnectServer;
begin
  with MainForm do
  begin
    if IdTCPClient.Connected then Exit;

    IdTCPClient.Host := OptionForm.IP;
    IdTCPClient.Port :=  OptionForm.Port;
    IdTCPClient.Connect(5000);

    try
      ReadingThread := TReadingThread.Create(IdTCPClient);
    except
      IdTCPClient.DisconnectSocket;
      raise;
    end;
  end;
end;

procedure TReadingThread.Execute;
begin
  try
    while not Terminated do
    begin
      FData.ReadFrom(FConn);
      if not Terminated then Synchronize(UpdateGrid);  //on exception here
    end;
  except
      Synchronize(MainForm.Terminate);
  end;
end;

If you need more code, let me know.

Replies:

In response to:

www.cryer.info
Managed Newsgroup Archive