×

Discussion Board

Results 1 to 2 of 2
  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    2

    Thread Update GUI

    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 :

    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);
    }
    Thread classe :
    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;
        }
    When I launch this code, I obtain a crash "Application Closed thread_id Kern-Exec 3"

    So, How can I change my label from my thread ?

  2. #2
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,737

    Re: Thread Update GUI

    Symbian GUI can not be accessed from any secondary threads. You need to implement IPC mechanisms (an active object as a minimum) if you want to send some message to the GUI from an other thread.

Similar Threads

  1. Emulator crash immediatly after launched, SDK of S60 3nd
    By loopfinder in forum Symbian Tools & SDKs
    Replies: 27
    Last Post: 2011-01-30, 14:06
  2. S60 5th ed emulator startup failed
    By hony in forum Symbian Tools & SDKs
    Replies: 8
    Last Post: 2009-08-11, 21:12
  3. S60 5th Edition SDK, EMULATOR crashing
    By zoiks_guy in forum Symbian Tools & SDKs
    Replies: 4
    Last Post: 2009-01-16, 05:37
  4. Can't start thread function
    By liuhoihing in forum Symbian
    Replies: 0
    Last Post: 2003-05-05, 06:35

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×