开发者

Problem with Symbian RThread

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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜