Jump to content

Displaying progressbar using threading in win 32 applicaition!

- - - - -

  • Please log in to reply
1 reply to this topic

#1
rakeshsr

rakeshsr

    Newbie

  • Members
  • Pip
  • 1 posts
Hi all,I am trying to display a progress bar with some informations in it,say for example am reading files ..I want to display the progress bar progressing with the filename that is being read etc..I am doing this in an win 32 application.I know that we should do it using thread but am not quite familiar with that,check the below picture example,thats what i need it exactly.

I tried the below,but its not working either i think its the not the right way to do.

     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

        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
deskchecked

deskchecked

    Newbie

  • Members
  • PipPip
  • 29 posts
I don't know MFC very well, but I think you need to break your problem up a little. In psuedo-Win32 -- with an algorithm that may or may not work -- you may want to try something along these lines:



WinMain() {

  for each file {

    WaitForThread(CreateThread(ReadFileWithProgress, file));

  }

}


ReadFileWithProgress(file) {

  dialog = CreateDialog()

  progress = CreateProgressBar(dialog);

  // TODO set up progress bar for STEPIT messages

  for (;;) {

    if (!Pump()) break;

    ReadChunk(file, dialog, progress);

  }

}


ReadChunk(file, dialog, progress) {

  if (ReadFile(file, buffer) == EOF) {

    PostMessage(dialog, WM_DESTROY, 0, 0);

  }

  else {

    PostMessage(progress, PBM_STEPIT, 0, 0);

  }

}


Pump() {

  result = true;

  while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {

    DispatchMessage(&msg);

    if (msg.message == WM_DESTROY) result = false;

  }

  return result;

}



At the very least, I hope this gives you some ideas. The most painful/ugly part is the link between threads and message queues. e.g. GetMessage(...) in WinMain will not see messages posted/sent in the ReadFileWithProgress thread. Otherwise you could probably hack it to work entirely independently of the main message pump.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users