CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Thread

  1. #1
    Join Date
    Mar 2008
    Posts
    18

    Thread

    Hi All

    Can any one give me thread tutorial?Actually i want to create a thread and call a function.
    Last edited by anubhava; October 7th, 2008 at 07:06 AM.

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Thread


  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Thread

    Victor Nijegorodov

  4. #4
    Join Date
    Mar 2008
    Posts
    18

    Re: Thread

    Quote Originally Posted by VictorN
    After read the artical.I am try to write one small example.I am try to call two function.One is calling through Timer and second one is simply call on ClickEvent.Can any one help me tolde it's right or wrong.
    Code:
    void CTestingDemoDlg::OnBnClickedButton1()
    {
    	running = TRUE;
    	AfxBeginThread(sun, this);
    	AfxBeginThread(sun1, this);
    	
    		
    }
    
    void CTestingDemoDlg::sun()
    {
    	OnStartTimer();
    	
    
    	
    
    }
    void CTestingDemoDlg::sun1()
    {
    	AfxMessageBox(_T("Hello"));
    }
    
    UINT CTestingDemoDlg::sun(LPVOID p)
    {
    	CTestingDemoDlg * me = (CTestingDemoDlg *)p;
    	me->sun();
         return 0;
    
    }
    void CTestingDemoDlg::OnStop()
    {
    	running = FALSE;
    }
    
    
    
    UINT CTestingDemoDlg::sun1(LPVOID p)
    {
    	CTestingDemoDlg * me = (CTestingDemoDlg *)p;
    	me->sun1();
         return 0;
    
    }
    
    void CTestingDemoDlg::OnTimer(UINT_PTR nIDEvent)
    {
    	// TODO: Add your message handler code here and/or call default
    
    	m_test.SetWindowTextW(_T("Sun"));
    	CDialog::OnTimer(nIDEvent);
    }
    void CTestingDemoDlg::OnStartTimer() 
    {
    
        SetTimer(1, 5000, 0);
    	
    }
    Thanks in advance

  5. #5
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Thread

    Code:
    UINT CTestingDemoDlg::sun(LPVOID p)
    {
    	CTestingDemoDlg * me = (CTestingDemoDlg *)p;
    	me->sun();
         return 0;
    
    }
    This isn't allowed. First of all, the main thread function needs to be static, and secondly, it is not safe to directly use functions from a dialog inside a thread. Also, calling yourself inside yourself ("me->sun()") is recursive and creates a stack overflow.

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Thread

    Well, this variant ("sun1"):
    Code:
    UINT CTestingDemoDlg::sun1(LPVOID p)
    {
    	CTestingDemoDlg * me = (CTestingDemoDlg *)p;
    	me->sun1();
         return 0;
    }
    is bad because it pops up the message box from the secondary (worker) thread, that you should (must!) avoid.

    The variant "sun" looks like not wrong, although I'd not start the timer directly from the worker thread. I'd PostMessage a user defined message to the main GUI thread notifying it that it could (or should) set a timer; then the main thread would choose whether or not set this timer.

    Note also, that after every new click of the "Button1" the two new threads will be started.
    Victor Nijegorodov

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured