Symbian RThread的问题

时间:2011-07-13 14:45:44

标签: c++ symbian nokia

我有这段代码:

void MyAppAppUi::ConstructL()
{
    _LIT(KString1, "asd");
    TBuf<15> buf1(KString1);
    TInt iStackSize = 32000;

    RThread iThread;

    TInt err = iThread.Create(
                buf1,
                func,
                iStackSize,
                NULL,
                NULL
               );
    iThread.Resume();
}
TInt func(TAny *obj)
{
    CAknInformationNote* note = new(ELeave)CAknInformationNote;
    TBuf<32> msg;
    msg.Format(_L(" rasdasd "));
    note->ExecuteLD(msg);
}

并在头文件中:

        friend TInt func(TAny *obj);

问题是它没有输入函数:func

错误等于KErrNone

1 个答案:

答案 0 :(得分:1)

我认为线程正在运行,但问题是你正在运行的是失败。

主要问题是您正在尝试在另一个线程中执行UI代码。这通常不起作用,在主线程上创建所有UI内容。所以你的代码永远不会工作。

此外,您还需要了解Symbian概念,例如资源管理(清理堆栈)和活动对象。

通常,当您启动Symbian线程时,您需要使用标准Symbian基础结构设置线程。您几乎总是需要在新线程上设置清理堆栈,您可能需要选择设置ActiveScheduler(在启动ActiveScheduler之前至少需要一个活动对象)。

有关如何创建和管理线程的信息,请参阅此Nokia RThread example

稍微打破一下这个例子:

您需要清理堆栈基础架构:

TInt ThreadFunction(TAny* aParams)
    {
    // 1. Add cleanup stack support.
    CTrapCleanup* cleanupStack = CTrapCleanup::New();

    // 2. Get pointer to thread host
    CMyThread* host = (CMyThread*)aParams;

    TRAPD(err,

... your code here ...

        );

    host->ThreadExecuted(err);
    delete cleanupStack;
    return KErrNone;
    }

如果您需要使用Active Objects,您还需要设置ActiveScheduler:

TInt ThreadFunction(TAny* aParams)
    {
    // 1. Add cleanup stack support.
    CTrapCleanup* cleanupStack = CTrapCleanup::New();

    // 2. Get pointer to thread host
    CMyThread* host = (CMyThread*)aParams;

    TRAPD(err,
        // 3. Add support for active objects
        CActiveScheduler* activeScheduler = new (ELeave) CActiveScheduler;
        CleanupStack::PushL(activeScheduler);
        CActiveScheduler::Install(activeScheduler);

        // 4. Create and start your active object here
..... you active object goes stuff here

        // NOTE: When adding CActiveScheduler support for threads we have to
        // add atleast one active object in it or it fails on 
        // CActiveScheduler::Start().
        // CPeriodic is derived from CActive active object so that is good for
        // this example.

        // 5. --> Thread execution starts
        CActiveScheduler::Start();
        // 6. --> Thread execution ends (waiting for CActiveScheduler::Stop())

        CleanupStack::PopAndDestroy(... your active object here....);
        CleanupStack::PopAndDestroy(activeScheduler);
        );

    host->ThreadExecuted(err);
    delete cleanupStack;
    return KErrNone;
    }