线程和进度栏问题 [英] Thread and progressbar issue
问题描述
我正在开发一个MFC基本应用程序,该应用程序从服务器读取数据并将其显示在列表控件中.
读取数据的时间需要20-25分钟,因此我使用辅助线程读取该数据.阅读部分在另一堂课中完成:
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 :
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;
}
我从对话框中隐藏列表控件和按钮,并在其中显示进度栏.
但是现在的问题是,如何增加进度栏?
1)我是否再使用一个增加进度条的线程
2)有什么方法可以在固定的时间间隔内将一个函数称为??
3)其他方式??
在此先感谢...
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 ...
推荐答案
在开始阅读之前,请通过发送 ^ ]消息.这将允许您设置栏的比例.
然后在您的读取线程中,发送 PBM_SETPOS [ ^ ]消息想要更新.例如,如果将范围设置为读取的数据的兆字节数,则每次读取该数量时都发送此消息.
如果某种程度上您的读取线程不允许这样做(例如,您使用一条语句而不是较小的块读取所有数据),则可以 ^ ],并每分钟发送一次PBM_SETPOS.但这不是一种非常准确的方法.
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.
可能是这样的:):
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;
}
这篇关于线程和进度栏问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!