MFC 下载线程中,下载完了关掉程序..急..

ryueiketu 2010-12-03 10:58:16
一个DLG中,画面初期化中起一个线程,用来下载,同时画面用来表示下载进度,当线程中下载完成后,我想关掉下载画面,同时整个程序结束...咋办?现在我是下载完了,程序结束不了..
代码:
UINT DownExec(LPVOID pParam)
{
CWnd *pwnd;
pwnd = CWnd::FromHandle((HWND)pParam);
CProgressCtrl *m_Prog = (CProgressCtrl*)pwnd->GetDlgItem(IDC_PROGRESS1);

CInternetSession netSession;
char filebuf[1024];
CStdioFile *fTargFile;
CString strFileUrl,strDFileName,strPocPer;
CString strUpdateFile;
long lngFileSize = 0L;
int intBytesWrite =0,intPoc =0,intPocPer =0;

BOOL isReadOK = FALSE;

strFileUrl = KINGSOFT_FILE_PATH;
strDFileName.Format(_T("%s\\%s"),GetApp()->GetAppDir(),KINGSOFT_FILE_NAME);

try{
pwnd->SetDlgItemTextW(IDC_STA_STATE,_T("Loading..."));
fTargFile = netSession.OpenURL(strFileUrl,1,INTERNET_FLAG_TRANSFER_BINARY | INTERNET_FLAG_RELOAD);
lngFileSize = fTargFile->SeekToEnd();
fTargFile->SeekToBegin();

long lngReadBytes = 0L;

CFile fDestFile(strDFileName, CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);

m_Prog->SetRange32(1,100);

while (intBytesWrite = fTargFile->Read(filebuf, 1024)){

isReadOK = TRUE;

lngReadBytes += intBytesWrite;
fDestFile.Write(filebuf, intBytesWrite);

m_Prog->SetPos((int)((double)lngReadBytes * 100.0/lngFileSize));
strPocPer.Format(_T("%d"),(int)((double)lngReadBytes * 100.0/lngFileSize));

pwnd->SetDlgItemText(IDC_STA_PRO,strPocPer + _T("%"));
}

if (isReadOK) {
intPocPer = 100;
strPocPer.Format(_T("%d"),intPocPer);
pwnd->SetDlgItemText(IDC_STA_PRO,strPocPer + _T("%"));
pwnd->SetDlgItemTextW(IDC_STA_STATE,_T("Downloaded"));
}

fDestFile.Close();
delete fTargFile;


((CDialog*)pwnd)->EndDialog(IDCLOSE);-->关掉对话框,退出程序,不好用?????

strUpdateFile.Format(_T("%s\\%s"), GetApp()->GetAppDir(), UPDATE_FILE);

//ShellExecute(NULL,L"open",strUpdateFile,L"",L"", SW_SHOW);

AfxEndThread(0);

}catch(CInternetException *IE){

}catch(CException *e){

}
return 0;
}
BOOL CDownLoadPro::OnInitDialog()
{
CWinThread* pThread = AfxBeginThread(DownExecKingsoft,GetSafeHwnd(),THREAD_PRIORITY_NORMAL);

return TRUE;
}
高手指点.....
...全文
89 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
ryueiketu 2010-12-03
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 visualeleven 的回复:]

((CDialog*)pwnd)->EndDialog(IDCLOSE);-->关掉对话框,退出程序,不好用?????
-->
::PostMessage(hWnd, WM_CLOSE, 0, 0);
hWnd为你AfxBeginThread传递的GetSafeHwnd参数的值
[/Quote]
我就知道,CSDN是个藏龙卧虎的地方,哈,用了你的方法,立刻解决....
看你牌牌就知道你是个高手...
接分..
新手上路,以后多多指教...
ryueiketu 2010-12-03
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 saylerboxer 的回复:]

下面几条都能关闭的,你的OnOK和OnCancel重载没有?
C/C++ code

OnOK();
OnCancel();
this->EndDialog(IDOK);
this->EndDialog(IDCANCEL);



另外你的这个循环能结束吗?执行到EndDialog那句吗?
C/C++ code

while (intBytesWrit……
[/Quote]
没有重载

能结束,文件对完就结束
ASUO1478 2010-12-03
  • 打赏
  • 举报
回复
((CDialog*)pwnd)->EndDialog(IDCLOSE);改成
pwnd->CloseWindow();就可以了。。
手机写程序 2010-12-03
  • 打赏
  • 举报
回复
非模式对话框要用DestroyWindow,而不是EndDialog.
yihandrensunyong 2010-12-03
  • 打赏
  • 举报
回复
TerminateThread(tHD,0);

tHD = INVALID_HANDLE_VALUE;

tHD是线程句柄。

线程如果没有关闭的话,会一直在里面,导致无法关闭主程序。
oyljerry 2010-12-03
  • 打赏
  • 举报
回复
要先把工作线程退出,然后再结束程序,退出主线程
Eleven 2010-12-03
  • 打赏
  • 举报
回复
((CDialog*)pwnd)->EndDialog(IDCLOSE);-->关掉对话框,退出程序,不好用?????
-->
::PostMessage(hWnd, WM_CLOSE, 0, 0);
hWnd为你AfxBeginThread传递的GetSafeHwnd参数的值
猞猁狲 2010-12-03
  • 打赏
  • 举报
回复
下面几条都能关闭的,你的OnOK和OnCancel重载没有?

OnOK();
OnCancel();
this->EndDialog(IDOK);
this->EndDialog(IDCANCEL);


另外你的这个循环能结束吗?执行到EndDialog那句吗?

while (intBytesWrite = fTargFile->Read(filebuf, 1024)){

isReadOK = TRUE;

lngReadBytes += intBytesWrite;
fDestFile.Write(filebuf, intBytesWrite);

m_Prog->SetPos((int)((double)lngReadBytes * 100.0/lngFileSize));
strPocPer.Format(_T("%d"),(int)((double)lngReadBytes * 100.0/lngFileSize));

pwnd->SetDlgItemText(IDC_STA_PRO,strPocPer + _T("%"));
}
hongkun18 2010-12-03
  • 打赏
  • 举报
回复
你要保证你的线程结束;
之后 你要还写程序退出的语句阿.

15,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧