Delphi threads deadlock

后端 未结 5 756
野性不改
野性不改 2020-12-23 18:10

I am having a problem sometimes with a deadlock when destroying some threads. I\'ve tried to debug the problem but the deadlock never seems to exist when debugging in the ID

5条回答
  •  暖寄归人
    2020-12-23 18:43

    Consider replacing Synchronize with a call to PostMessage and handle this message in the form to add a log message to the memo. Something along the lines of: (take it as pseudo-code)

    WM_LOG = WM_USER + 1;
    ...
    MyForm = class (TForm)
      procedure LogHandler (var Msg : Tmessage); message WM_LOG;
    end;
    ...
    PostMessage (Application.MainForm.Handle, WM_LOG, 0, PChar (LogStr));
    

    That avoids all the deadlock problems of two threads waiting for each other.

    EDIT (Thanks to Serg for the hint): Note that passing the string in the described way is not safe since the string may be destroyed before the VCL thread uses it. As I mentioned - this was only intended to be pseudocode.

提交回复
热议问题