在活动对象内运行线程

发布于 2024-12-13 10:55:42 字数 1347 浏览 2 评论 0原文

我有一个从 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 技术交流群。

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

发布评论

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

评论(1

欢烬 2024-12-20 10:55:42

活动对象本质上是线程特定的,因为它们依赖于线程信号量来发出信号(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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文