title: Delphi call a function in anonymous thread?
body: <p>I have to call a function in a anonymous thread in a while</p>
  
  <p>my sample function is like this, just for print output:</p>
  
  <pre><code>function processPureTmFrame(rowFrame : string;tmDataGroupRef:string ):string;
      TThread.Synchronize(nil,
        procedure
        begin
           form2.Memo1.Lines.Add( tmSlitFrame );
        end
      );      
  end;
  </code></pre>
  
  <p>when i call the function like this :</p>
  
  <p><strong>code1</strong></p>
  
  <pre><code>while tmBody.Length&gt;0 do
  begin
      tmBodyFrameLength := ((hextodec( copy(tmBody,11,2) )+6)*2)+2;
      tmSplitFrame :=  copy(tmBody , 1 , tmBodyFrameLength );
      delete( tmBody, 1, tmBodyFrameLength );
  
      myThread := TThread.CreateAnonymousThread(
      procedure
      begin
          processPureTmFrame( tmSplitFrame , tmDataGroupRef );
      end);
      myThread.Start;
  end;
  </code></pre>
  
  <p>in the first cycle of loop the output is missing</p>
  
  <p>but when i call my code without thread every thing is ok!!</p>
  
  <p><strong>code2</strong> </p>
  
  <pre><code>while tmBody.Length&gt;0 do
  begin
      tmBodyFrameLength := ((hextodec( copy(tmBody,11,2) )+6)*2)+2;
      tmSplitFrame :=  copy(tmBody , 1 , tmBodyFrameLength );
      delete( tmBody, 1, tmBodyFrameLength );
      processPureTmFrame( tmSplitFrame , tmDataGroupRef );
  end;
  </code></pre>
  
  <p>the correct output must be like this</p>
  
  <pre><code>0851C007000C010100000007581850C001F116
  0836C0BE001003627169DCA200000000000090D72AACAF
  0814C0B6001C03197169DCA31901E2041211131D001F00001F1E1C1F1F1E1E1E0077AA
  0814C0B7001E03197169DCA31902FE00540F0000000000000000000000000000000000E238
  0814C0B8000B03197169DCA31903FE01384E
  0817C0B9000D05017169DCA3E6010190B03F042D
  0852C000000B036200000000FAFFFFBF16A3
  0852C001000B036200000001F4FF00000000
  </code></pre>
  
  <p>but when call in thread(code 1) its is like </p>
  
  <pre><code>0836C0BE001003627169DCA200000000000090D72AACAF
  0814C0B6001C03197169DCA31901E2041211131D001F00001F1E1C1F1F1E1E1E0077AA
  0814C0B7001E03197169DCA31902FE00540F0000000000000000000000000000000000E238
  0814C0B8000B03197169DCA31903FE01384E
  0817C0B9000D05017169DCA3E6010190B03F042D
  0852C000000B036200000000FAFFFFBF16A3
  0852C001000B036200000001F4FF00000000
  </code></pre>
  
  <p>without thread(code 2) output is ok</p>
  
  <p><strong>Note #1</strong>: I don't get any error like:</p>
  
  <blockquote>
    <p>System Error. Code:1400. Invalid window handle or any thing else</p>
  </blockquote>
  
  <p><strong>Note #2</strong>: as I said just first cycle of while not sending to the new threads. Other lines are sending and processing just fine!</p>