With what delphi Code should I replace my calls to deprecated TThread method Suspend?

后端 未结 4 1368
礼貌的吻别
礼貌的吻别 2020-12-13 15:55

It has been asked before, but without a full answer. This is to do with the so called famous \"‘Fatal threading model!’\".

I need to replace this call to TThread.Su

4条回答
  •  旧巷少年郎
    2020-12-13 15:55

    Your code uses a Windows event handle, it should better be using a TEvent from the SyncObjs unit, that way all the gory details will already be taken care of.

    Also I don't understand the need for a waiting time - either your thread is blocked on the event or it isn't, there is no need for the wait operation to time out. If you do this to be able to shut the thread down - it's much better to use a second event and WaitForMultipleObjects() instead. For an example see this answer (a basic implementation of a background thread to copy files), you only need to remove the code dealing with file copying and add your own payload. You can easily implement your Start() and Stop() methods in terms of SetEvent() and ResetEvent(), and freeing the thread will properly shut it down.

提交回复
热议问题