Symbian RThread 问题

发布于 2024-11-24 02:25:39 字数 680 浏览 3 评论 0原文

我有这样的代码:

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

err 等于 KErrNone

I have this code:

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);
}

and in the header file this:

        friend TInt func(TAny *obj);

The problem is that it doesn't enter in the function: func

err is equal to KErrNone

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

新雨望断虹 2024-12-01 02:25:39

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

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

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

一般来说,当您启动 Symbian 线程时,您需要使用标准 Symbian 基础设施来设置该线程。您几乎总是需要在新线程上设置一个清理堆栈,并且您可能需要选择设置一个 ActiveScheduler(尽管在启动 ActiveScheduler 之前至少需要一个活动对象)。

请参阅此 Nokia RThread 示例,了解如何创建和管理线程。

稍微分解一下示例:

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

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;
    }

如果您需要使用活动对象,您还需要设置 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;
    }

I think that the thread is running, but the problem is what you are running is failing.

The main problem is that you are trying to do UI code in another thread. This will generally not work, create all UI stuff on your main thread. So your code will never work anyway.

Also you need to learn about Symbian concepts like the there resource management (cleanup stack) and active objects.

Generally, when you start a Symbian thread you need to setup the thread with standard Symbian infrastructure. You almost always need to setup a cleanup stack on the new thread, and you may need to optionally setup a ActiveScheduler (altho you need at least one active object before you start the ActiveScheduler).

See this Nokia RThread example on how to create and manage a thread.

To break the example down a little:

You need the cleanup stack infrastructure:

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;
    }

If you need to use Active Objects you need to also setup the 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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文