Hi,
I have a (simple?) problem, but i didn't found any solution.
I hava a thread which contains a server socket.
this server wait for a client, when a client comming, I would like to inform the user (on the screen).
How can I change a label text, for example, from a thread ?
this is a portion of my code :
Thread classe :Code:Main GUI Class : //Launch Thread TBool CTestComSocketContainerView::HandleConnectionMenuItemSelectedL( TInt aCommand ) { ExecuteWaitDialog1LD(_L("debut connection")); TTimeIntervalMicroSeconds32 time(100); SocketFlash * iMyThread = SocketFlash::NewL(*this); iMyThread->ExecuteThread(); return ETrue; } //Event receive from thread to indicate there is a new client void CTestComSocketContainerView::ThreadNewMess(const TDesC& aMess) { this->iLabel1->SetTextL(aMess); }
When I launch this code, I obtain a crash "Application Closed thread_id Kern-Exec 3"Code:#include "SocketFlash.h" const TInt KStackSize = 16384; _LIT(KExecThreadBaseName, "SocketFlash"); // Global thread id counter for making SocketFlash thread names unique. // This is achieved by appending counter to the end of thread name and // incrementing counter value for next thread. // This is writable static data. TInt g_thread_id_counter = 0; LOCAL_C void GenerateUniqueName(TDes& aResult, SocketFlash* aExecutor) { _LIT(KHexFormat, "_0x%x"); _LIT(KCounterFormat, "_%d"); aResult.Copy(KExecThreadBaseName); aResult.AppendFormat(KHexFormat, aExecutor); g_thread_id_counter++; aResult.AppendFormat(KCounterFormat, g_thread_id_counter); } SocketFlash* SocketFlash::NewL(MThreadExecuteObserver& aObserver) { SocketFlash* self = new (ELeave) SocketFlash(aObserver); CleanupStack::PushL(self); self->ConstructL(); CleanupStack::Pop(self); return self; } SocketFlash::SocketFlash(MThreadExecuteObserver& aObserver) : CActive(EPriorityStandard),iObserver(aObserver) { CActiveScheduler::Add(this); } SocketFlash::~SocketFlash() { Cancel(); iThread.Close(); } void SocketFlash::ConstructL() { } TInt SocketFlash::ExecuteThread() { TInt ret = KErrNone; //iInterval = anInterval; TRAP(ret,CreateThreadL()); if (!ret) { ret = StartThread(); } return ret; } TInt SocketFlash::StartThread() { TInt ret = KErrNone; if(!IsActive()) { // Requests notification when this thread dies // normally or otherwise iThread.Logon(iStatus); SetActive(); iThread.Resume(); } else { ret = KErrOverflow; } return ret; } void SocketFlash::ThreadExecuted(TInt aError) { iObserver.ThreadExecuted(aError); } void SocketFlash::ThreadNewMess(const TDesC& aMess) { iObserver.ThreadNewMess(aMess); } void SocketFlash::RunL() { iObserver.ThreadExecuted(iStatus.Int()); } void SocketFlash::DoCancel() { iThread.LogonCancel(iStatus); iThread.Kill(KErrCancel); } void SocketFlash::CreateThreadL() { HBufC* threadName = HBufC::NewLC(KMaxFileName); TPtr ptr = threadName->Des(); GenerateUniqueName(ptr, this); User::LeaveIfError(iThread.Create( *threadName, SocketFlash::ThreadFunction, KStackSize, NULL, this)); CleanupStack::PopAndDestroy(threadName); } TInt SocketFlash::ThreadFunction(TAny* aParams) { // 1. Add cleanup stack support. CTrapCleanup* cleanupStack = CTrapCleanup::New(); // 2. Get pointer to thread host SocketFlash* host = (SocketFlash*)aParams; RSocketServ ss; RSocket sock; host->ThreadNewMess(_L("Launch Socket")); //Création d'une socket client TInt err; err = ss.Connect(); err = sock.Open(ss, KAfInet, KSockStream, KProtocolInetTcp); const TUint32 KInetAddr = INET_ADDR(127,0,0,1); const TUint32 KMyPort = 18000; TSockAddr iAddress; iAddress.SetPort(KMyPort); User::LeaveIfError(sock.Bind(iAddress)); User::LeaveIfError(sock.Listen(1)); RSocket accept; TRequestStatus status = KRequestPending; while(1) { User::LeaveIfError(accept.Open(ss)); status = KRequestPending; sock.Accept(accept, status); User::WaitForRequest(status); if( status == KErrNone ) { TBuf8<20> iBuffer; _LIT(KFlagServer3, "Ready to receive ...\n"); host->ThreadNewMess(KFlagServer3); status = KRequestPending; accept.Recv(iBuffer, 0, status); User::WaitForRequest(status); if( status == KErrNone ) { TBuf16<100> iwBuffer; iwBuffer.Copy(iBuffer); _LIT(KFlagServer5, "Received : "); host->ThreadNewMess(KFlagServer5); host->ThreadNewMess(iwBuffer); } else { _LIT(KFlagServer7, "An error occured with recv()!\n"); host->ThreadNewMess(KFlagServer7); break; } } else { _LIT(KFlagServer6, "An error occured with accept()!\n"); host->ThreadNewMess(KFlagServer6); break; } accept.Close(); } accept.Close(); sock.Close(); host->ThreadNewMess(_L("Socket Close")); host->ThreadExecuted(err); delete cleanupStack; return KErrNone; }
So, How can I change my label from my thread ?

Reply With Quote

