-1

I have a problem about adding string to Tmemo using TThread.ShowMessage can Show this string.The Application doesn't give any error about adding string to Tmemo but It doesn't be added to TMemo.So here is my code:

procedure TThreadGet.Execute;
var
Filed:TStringList;
begin
Filed:=TStringList.Create;
Filed.LoadFromFile(Username1+'.dat');
Messaged:=Touser+':'+Filed.Text;
Form2.Memo1.Lines.Add(Messaged);//Doesn't give error.But Doesn't Add String.
Showmessage(Messaged);//Shows String Right.
end;
Filed.Free;
Terminate;
end;
Srs
  • 36
  • 6
  • 3
    Look at the top of your thread unit. You should find an IDE-inserted warning that you can't access/update a VCL component in any thread except your app's main one Read up on and use Synchronize instead. – MartynA Jul 13 '14 at 07:20
  • But I can Edit Components Using Threads like Label or etc. – Srs Jul 13 '14 at 07:26
  • No, you've just been lucky. Don't take my word for it, there are dozens of questions here on SO from people who don't see or ignore the warning and run into the problem you have. Even when you're aware of this you can still get problems - see e.g. http://stackoverflow.com/questions/17705197/thread-safe-in-delphi which btw has quite a good set of links in the answer. – MartynA Jul 13 '14 at 07:31
  • 2
    @Martyn I'd say he has been unlucky. Better to find out sooner or later. – David Heffernan Jul 13 '14 at 07:33
  • @David Heffernan: Arf. I was just going to dig out the OP a link to Rob James' seminal document on threading. – MartynA Jul 13 '14 at 07:36
  • Actually I was muddling Rob James up with Martin James and him with Martin Harvey. The Martins used to post frequently in the Borland as was NGs about threads. It was MH who wrote the paper I meant: http://thaddy.co.uk/threads/ – MartynA Jul 13 '14 at 07:55
  • @MartynA, Martin Harvey is working on part II of his multithreading tutorial, https://forums.codegear.com/message.jspa?messageID=611049. – LU RD Jul 13 '14 at 08:16
  • 1
    Is there a badge for posting the most repeated question on a topic? This has to be at least the millionth one that ignores that huge comment block at the start of a new thread unit, all of the documentation, and the millions of posts here and at every Delphi site that screams **you can't use VCL components in threads without Sycnronize or Queue**. – Ken White Jul 13 '14 at 14:04
  • 2
    FWIW, I've never seen that comment because I never make a thread that way – David Heffernan Jul 13 '14 at 14:14

1 Answers1

3

All access to VCL components must be performed in the main UI thread. For instance you may use TThread.Synchronize or TThread.Queue to arrange this.

The main reason for this is that Win32 windows have thread affinity. They also are only safe to access from the thread that created them. This property gives a very strong push to single thread UI and the VCL design goes this way.

Multi-threaded UI is possible in Win32 although it is much more tricky to do correctly. The VCL does not support that at all.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490