CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2009
    Posts
    6

    Displaying progressbar using threading in win 32 applicaition!

    In my application I have a simple module were I will read files for some process that will take
    few seconds..so I thought of displaying a progress bar(using worker thread) while the files are in progress.I have created a thread (code shown below) and also I designed a dialog window with progress control.I used the function MyThreadFunction below to display the progressbar but it just shows only one time and disappears,I am not sure how to make it work.I tried my best inspite of the fact that I am new to threading.





    reading files
    void ReadMyFiles()
    {

    for(int i = 0; i < fileCount ; fileCount++)
    {
    CWinThread* myThread = AfxBeginThread((AFX_THREADPROC)MyThreadFunction,NULL);
    tempState = *(checkState + index);
    if(tempCheckState == NOCHECKBOX)
    {
    //my operations
    }
    else//CHECKED or UNCHECKED
    {
    //myoperation
    }
    myThread->PostThreadMessage(WM_QUIT,NULL,NULL);
    }
    }

    thread functions
    Expand|Select|Wrap|Line Numbers

    UINT MyThreadFunction(LPARAM lparam)
    {
    HWND dialogWnd = CreateWindowEx(0,WC_DIALOG,L"Proccessing...",WS_OVERLAPPEDWINDOW|WS_VISIBLE,
    600,300,280,120,NULL,NULL,NULL,NULL);
    HWND pBarWnd = CreateWindowEx(NULL,PROGRESS_CLASS,NULL,WS_CHILD|WS_VISIBLE|PBS_MARQUEE,40,20,200,20,
    dialogWnd,(HMENU)IDD_PROGRESS,NULL,NULL);

    MSG msg;

    PostMessage( pBarWnd, PBM_SETRANGE, 0, MAKELPARAM( 0, 100 ) );
    PostMessage(pBarWnd,PBM_SETPOS,0,0);
    while(PeekMessage(&msg,NULL,NULL,NULL,PM_NOREMOVE))
    {
    if(msg.message == WM_QUIT)
    {
    DestroyWindow(dialogWnd);
    return 1;
    }
    AfxGetThread()->PumpMessage();
    Sleep(40);
    }
    return 1;


    }

  2. #2
    Join Date
    Feb 2005
    Posts
    2,160

    Re: Displaying progressbar using threading in win 32 applicaition!

    Please use code tags!

    It looks like you're creating a new window in your thread which is almost always a bad thing. You should keep all your UI code in the main thread and signal it from your worker thread by posting messages to the UI from your worker thread.

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

    Re: Displaying progressbar using threading in win 32 applicaition!

    hoxsiew is right.
    I'd only like to add that if you in the future would need a secondary thread with a message pump (to receive/handle thread messages) you should create so called "UI" thread using the overloaded:
    Code:
    CWinThread* AfxBeginThread( 	CRuntimeClass* pThreadClass, 
    				int nPriority = THREAD_PRIORITY_NORMAL, 
    				UINT nStackSize = 0, 
    				DWORD dwCreateFlags = 0, 
    				LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL );
    Victor Nijegorodov

Tags for this Thread

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