在活动对象内运行线程
我有一个从 CTimer 类派生的类。我有一个 RThread 实例作为数据成员来定期调用 CTimer::After() 方法。代码是:
void CTimerThread::RunL()
{
qDebug() << "Value=" << ++iCounter;
if (iThread->ExitType() == EExitKill)
{
if (KErrNone == CreateThread())
iThread->Resume();
}
}
void CTimerThread::StartL()
{
qDebug() << "In the StartL( );";
if(isThreadCreated == EFalse)
User::LeaveIfError(CreateThread ());
iThread->Resume();
}
TInt CTimerThread::ThreadFunction(TAny *sender)
{
CTrapCleanup* cleanupStack = CTrapCleanup::New();
CTimerThread* host = (CTimerThread*)sender;
forever {
host->After(host->iInterval->Int());
if (!host->isSchedulStarted)
{
CActiveScheduler::Start();
host->isSchedulStarted = ETrue;
}
}
delete cleanupStack;
return 1;
}
TInt CTimerThread::CreateThread()
{
TInt err = KErrNone;
_LIT(KNameBase, "Thread_");
TBuf<10> name(KNameBase);
name.AppendNum(iCounter);
err = iThread->Create(name, CTimerThread::ThreadFunction, 4096, NULL, this);
if( err == KErrNone)
isThreadCreated = ETrue;
return err;
}
当我执行 StartL() 时,我总是得到发生数据中止异常。问题是什么?
I have a class which is derived from CTimer class. I have an instance of RThread as data member to periodically invoke CTimer::After() method. The code is:
void CTimerThread::RunL()
{
qDebug() << "Value=" << ++iCounter;
if (iThread->ExitType() == EExitKill)
{
if (KErrNone == CreateThread())
iThread->Resume();
}
}
void CTimerThread::StartL()
{
qDebug() << "In the StartL( );";
if(isThreadCreated == EFalse)
User::LeaveIfError(CreateThread ());
iThread->Resume();
}
TInt CTimerThread::ThreadFunction(TAny *sender)
{
CTrapCleanup* cleanupStack = CTrapCleanup::New();
CTimerThread* host = (CTimerThread*)sender;
forever {
host->After(host->iInterval->Int());
if (!host->isSchedulStarted)
{
CActiveScheduler::Start();
host->isSchedulStarted = ETrue;
}
}
delete cleanupStack;
return 1;
}
TInt CTimerThread::CreateThread()
{
TInt err = KErrNone;
_LIT(KNameBase, "Thread_");
TBuf<10> name(KNameBase);
name.AppendNum(iCounter);
err = iThread->Create(name, CTimerThread::ThreadFunction, 4096, NULL, this);
if( err == KErrNone)
isThreadCreated = ETrue;
return err;
}
When I execute StartL() I always get a data abort exception has occured. what is the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
活动对象本质上是线程特定的,因为它们依赖于线程信号量来发出信号(
User::WaitForRequest()
、User::RequestComplete()
等)。您不能直接调用另一个线程的活动对象。另一个问题:您的线程没有安装活动调度程序。如果您计划在新创建的线程中使用活动对象,请首先使用
CActiveScheduler::Install()
活动调度程序。Active objects are inherently thread-specific since they rely on the thread semaphore for signaling (
User::WaitForRequest()
,User::RequestComplete()
etc.). You cannot directly invoke active objects of another thread.Another issue: your thread does not have an active scheduler installed. If you plan to use active objects in a newly created thread,
CActiveScheduler::Install()
an active scheduler first.