C++ Symbian RThread的问题
我有以下代码:C++ Symbian RThread的问题,c++,symbian,nokia,C++,Symbian,Nokia,我有以下代码: void MyAppAppUi::ConstructL() { _LIT(KString1, "asd"); TBuf<15> buf1(KString1); TInt iStackSize = 32000; RThread iThread; TInt err = iThread.Create( buf1, func, iStac
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);
}
问题是它没有进入函数:func
err等于KErrNone我认为线程正在运行,但问题是您正在运行的线程失败了 主要问题是,您试图在另一个线程中编写UI代码。这通常不起作用,在主线程上创建所有UI内容。所以你的代码无论如何都不会工作 您还需要了解Symbian的概念,如there资源管理(清理堆栈)和活动对象 通常,启动Symbian线程时,需要使用标准Symbian基础结构设置线程。您几乎总是需要在新线程上设置清理堆栈,并且可能需要选择性地设置ActiveScheduler(尽管在启动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,
... 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;
}
看起来您正在堆栈上分配thread对象,这可能是个坏主意,除非您显式地等待线程完成。我的猜测是,当
ConstructL
返回并且iThread
对象被销毁时,线程就会停止。
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;
}