Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am developing one MFC base application that reads data from server and display it in list control.

Time to read data takes 20-25 minutes so i use worker thread to read that data. Reading part is done in another class :
C++
UINT ThreadProc(LPVOID param);

// OnInitDialog
m_Progress.SetRange32( 0, 100 );// Progress bar
    m_Progress.SetStep( 1 );
    m_Progress.SetPos( 0 );
    ::ShowWindow(m_listCtrl.GetSafeHwnd(),SW_HIDE);// Control
    ::ShowWindow(m_Btn.GetSafeHwnd(),SW_HIDE);//Buttons
    CWinThread *pThread = AfxBeginThread(::ThreadProc, (LPVOID)this);
    if(!pThread)
    {
        m_bRunning = FALSE;
        return FALSE;
    }
    m_hWorkerThread = pThread->m_hThread;

//ThreadProc Function
UINT ThreadProc(LPVOID param)
{
	CDlg *pDlg = (CDlg *)param;
	pDlg->OnIncProgress(0,0);
	return 0;
}

LRESULT CDlg::OnIncProgress( WPARAM, LPARAM )
{
	CData data;
        data.readfile();
	return 1;
}

I hide list control and buttons from the dialog and show progress bar in it.

But now problem is that how do I increment my progress bar ??
1) Do i use one more thread that increment progress bar
2) Is there any way that for a fixed interval of time a function get called ??
3) Any other way ??

Thanks in advance ...
Posted
Updated 3-Dec-10 5:40am
v3

Before you begin the reading, initialise the range of your progressbar by sending a PBM_SETRANGE [^]message. This will allow you to set the scale for your bar.

Then in your read-thread, send a PBM_SETPOS[^] message every time you want an update. For example if you set your range to the number of megabytes of data read, send this message every time you've read that amount.

If somehow your read-thread doesn't allow for this (e.g. you read all your data with one statement instead of in smaller chunks) you could set a timer[^] and send a PBM_SETPOS every minute. This isn't a very accurate way to do it though.
 
Share this answer
 
v2
Comments
ShilpiP 3-Dec-10 8:51am    
Hi Thaddeus Jones,
Thanks for your reply. There is one more thing i missed to share is that the reading is done in another class not in dialog class..i create object of that class and call that function :) Sorry its my mistake.
I set my range and position in oninitdialog :) but now handle goes to another class when function call.
ShilpiP 3-Dec-10 14:48pm    
Hey its done now Thanks :)
It could be something like this :) :
BEGIN_MESSAGE_MAP(CYourDialog, CDialog)
...
ON_MESSAGE(WM_USER, OnProgress)
...
END_MESSAGE_MAP()

LONG CYourDialog::OnProgress(WPARAM wParam, LPARAM lParam)
{
  int iCur = (int) wParam;
  int iEnd = (int) lParam;

  if (iEnd) {
    m_ctlProgress.SetRange32(0, iEnd);
  } else {
    m_ctlProgress.SetPos(iCur);
  }

  return 0;
}

/*static*/ UINT CYourDialog::ThreadProc(void* pcDlgPointer)
{
  CYourDialog* pcDlg = (CYourDialog*) pcDlgPointer;

  if (pcDlg->GetSafeHwnd()) {
    CYourData cData;
    cData.Init(..);

    int iLen = cData.Length();
    pcDlg->PostMessage(WM_USER, (WPARAM) 0, (LPARAM) iLen);

    int iCurPos(0);
    BYTE byBuffer[CYourData::sm_iMaxLengthPerStep];

    while (pcDlg->IsRunning() && iCurPos < iLen) {
      iCurPos += cData.GetNext(iCurPos, byBuffer, CYourData::sm_iMaxLengthPerStep);
      ...
      pcDlg->PostMessage(WM_USER, (WPARAM) iCurPos, (LPARAM) 0);
      Sleep(0);
    }
  }

  return 0;
}
 
Share this answer
 
v4
Comments
ShilpiP 3-Dec-10 14:02pm    
Dear Eugen Podsypalnikov ,
Thanks for your reply.
No the function ThreadProc is not like that in my condition :((
YouData cdata;
data.getdata();
Yourdata is a class that open file, read the content and update it in a list and that list i get here and display it in a list control.
there is no while loop in my threadproc.if it is so than it is very easy for me to implement.
ShilpiP 3-Dec-10 14:51pm    
Hi Eugen,
The problem is solved now.I pass the dlg handle to that function :) Thanks again..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900