ShowModal on onAccept of TTCPServer causes the application to hang?

615 views Asked by At

I'm programing one network application in delphi 6 using TTCPServer. At OnAccept event of this component I call showmodal for another form. This cause to hang the main form. I think that it is because of threads but I do not know how to showmodal the form at this event. I really have to call that. I appreciate any answer. thanks.

var
  s: array[0..10000] of byte;
  i, j: integer;
  Str : String;
  Request, UN, Pass: WideString;
  StartItemNode : IXMLNode;
  st: TStringStream;
begin
  CoInitialize(nil);

  i := ClientSocket.ReceiveBuf(s, 10000, 0);

  Str := '';
  for j := 0 to i - 1 do
    Str := Str + AnsiChar(s[j]);

  XMLDoc.XML.Text := Str;
  XMLDoc.Active := true;
  StartItemNode := XMLDoc.ChildNodes.FindNode('r');
  Request := StartItemNode.ChildNodes['request'].Text;

  if(Request = 'Order')then
  begin
    Memo1.Lines.Text := Str;
    ClientSocket.Sendln('<?xml version="1.0" encoding="utf-8"?><r><answer result="OK"></answer></r>');

    **Form2.ShowModal;**
  end;
1

There are 1 answers

0
hamid On BEST ANSWER

Finally find the answer. with Indy component we can use TidSync or TidNotify. Create it and the call the Synchronize method of then with parameter of a method. this method with run at main thread(UI) and it is thread safe. I can not find any solution for TTCPServer but we can use Timer. Set its Interval to for example 100 and do your showmodal at tick of that. but do not forgot about more than one running at same time. for example at OnTimer you have to disable recieving new connections or save them in array to process next OnTimer event.