Results 1 to 4 of 4

Thread: multithread problem

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2004
    Posts
    222

    multithread problem

    Why the following codes works first "for loop" and then "progressbar" but not parallerly?



    Code:
    void CProgressbarDlg::OnButton1() 
    {
    hThread=AfxBeginThread(ThreadFunc,&m_Progress,THREAD_PRIORITY_HIGHEST);
    
    
       for(int i=0;i<50000;i++)
       {
    		m_edit=m_edit+"C";
    		
    		UpdateData(FALSE);
       }
       
    //   hThread->SuspendThread();
       
    }
    
    
    UINT ThreadFunc(LPVOID param1) 
    { 
       CProgressCtrl *param=(CProgressCtrl*)param1;
       BOOL d=TRUE;
    	param->SetRange(1,10);
    	while(1)
    	{
    		if (d) 	
    		{
    			param->SetPos(param->GetPos()+1);
    			if (param->GetPos()==10)
    				d=FALSE;
    		}
    		else
    		{
    			param->SetPos(param->GetPos()-1);
    			if (param->GetPos()==1)
    				d=TRUE;
    		}
    		Sleep(500);
    	}
    	return 0;
    }

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: multithread problem

    Read about MFC and multithreading. You can't reuse MFC objects across threads. It's simply not possible.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: multithread problem

    I'm pretty sure I something like this before, and it worked fine.

    Did you try changing the thread priotity from THREAD_PRIORITY_HIGHEST to THREAD_PRIORITY_NORMAL, and see what hapens ?

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Mar 2004
    Posts
    222

    Re: multithread problem

    Yes I tried it.
    But if I create two more threds besides the base thred.
    then the child threds work parallely.

    But I am curious to know why do I need two more threds if I need only one more thred.

    But in console programs it works fine.

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