我有这个代码:
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
最佳答案
我认为线程正在运行,但问题是您正在运行的程序失败了。
主要问题是您正试图在另一个线程中执行 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;
}
如果您需要使用事件对象,您还需要设置 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;
}
关于c++ - Symbian RThread 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6680782/