0

我对多线程没有什么问题。我使用 CreateThread 创建自己的线程并在程序启动时创建它(抱歉,目前我不能使用 VCL 线程)。所以我的线程使用我的 VCL 表单。所有程序生命的第二线程生命也是如此。但这里有一个问题。当 VCL 表单要终止我的线程时,可以检查一些表单(类)参数。当然,当我的主表单已经终止并且某些线程尝试检查此表单中的方法时......然后我遇到了访问冲突。

如何在我的 VCL 表单中保护检查参数?谢谢!

这是我的代码。

unsigned int WINAPI CheckMutex( LPVOID lpParam )
{
    const int def = 20;
    int Cnt = def;
    UnicodeString text;
    while (1)
    {
        if (!UpdFrm || !UpdFrm->Label8 || UpdFrm->MutexTerminate)
            break;

首先我检查指向 UpdFrm 的指针,但 VCL 表单可以终止,但指向表单的指针仍然存在。所以这就是为什么我检查一些现有的控件。只有在那之后我才检查 MutexTerminate

4

1 回答 1

0

But here one problem. When VCL form going to terminate my thread can check some form (class) params

Don't do this, for exactly the reason you have found. Do not access directly any form instance vars from your secondary worker threads.

If you have to communicate with GUI-thread VCL components, or TForm descendant instance vars, do so only via Windows messages, preferably PostMessaged to the form.

The only other way round this issue is to ensure that the secondary thread is terminated before the form instance is freed. This will lead you into a maze of twisty little deadlocks, all alike :(

于 2013-05-02T18:12:20.703 回答