Click here to Skip to main content
15,914,111 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Saving to PDF Pin
Andy Latham25-Nov-01 22:07
Andy Latham25-Nov-01 22:07 
General2 Questions Pin
mvworld23-Nov-01 3:07
mvworld23-Nov-01 3:07 
GeneralRe: 2 Questions Pin
Joaquín M López Muñoz23-Nov-01 3:35
Joaquín M López Muñoz23-Nov-01 3:35 
GeneralChanging System Icon in Dialog Window Pin
John Clump22-Nov-01 22:04
John Clump22-Nov-01 22:04 
GeneralRe: Changing System Icon in Dialog Window Pin
Nish Nishant22-Nov-01 22:16
sitebuilderNish Nishant22-Nov-01 22:16 
QuestionA question about bitmap ? Pin
Leesen22-Nov-01 21:57
Leesen22-Nov-01 21:57 
AnswerRe: A question about bitmap ? Pin
Nish Nishant22-Nov-01 22:27
sitebuilderNish Nishant22-Nov-01 22:27 
GeneralA bug concerning 2 threads in my program Pin
22-Nov-01 21:13
suss22-Nov-01 21:13 
Hi there! Below is my problematic dialog-based program that has 2 threads : the program's primary thread and a second thread.
This program has a pushbutton, and when clicked, a modal dialog box pops up.
The program downloads a specified file from the Internet to the user's hard disk.

Name of main program dialog class : CDownloaderDlg
Name of modal dialog class : CUpdateDlg

Below, is my code for showing the modal dialog box when the pushbutton is clicked in the main dialog :


"#include "UpdateDlg.h"
void CDownloaderDlg::OnUpdatenow()
{
//show the modal dialog box
CUpdateDlg dlg(this);
dlg.DoModal();
}


Now, after running the code above, a modal dialog box(CUpdateDlg) will pop up, right?


//*******Below is all my code for the UpdateDlg.cpp file.*********

//I link this program with the WinInet library, cos' it's required
// UpdateDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Downloader.h"
#include "UpdateDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

//include afxinet.h header file
#include <afxinet.h>

const UINT WM_HTTPDOWNLOAD_THREAD_FINISHED = WM_APP + 1;

/////////////////////////////////////////////////////////////////////////////
// CUpdateDlg dialog


CUpdateDlg::CUpdateDlg(CWnd* pParent /*=NULL*/)
: CDialog(CUpdateDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CUpdateDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT

//m_pThread is a CWinThread member variable
m_pThread = NULL;
//m_bSafeToClose is a BOOL member variable
m_bSafeToClose = FALSE;
}


void CUpdateDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CUpdateDlg)
DDX_Control(pDX, IDC_STATUS_PERCENTAGE, m_Status_Percentage);
DDX_Control(pDX, IDC_STATUS_KB, m_Status_KB);
DDX_Control(pDX, IDC_DOWNLOADSTATUS, m_DownloadStatus);
//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CUpdateDlg, CDialog)
//{{AFX_MSG_MAP(CUpdateDlg)
ON_BN_CLICKED(IDC_DOWNLOAD, OnDownload)
ON_WM_DESTROY()
ON_WM_CLOSE()
//}}AFX_MSG_MAP
ON_MESSAGE(WM_HTTPDOWNLOAD_THREAD_FINISHED, OnThreadFinished)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CUpdateDlg message handlers

LRESULT CUpdateDlg::OnThreadFinished(WPARAM wParam, LPARAM /*lParam*/)
{
m_bSafeToClose = TRUE;

EndDialog(IDCANCEL);

return 0L;
}

//function for downloading a file from the Internet
//you don't realy need to know this function, though.
CString CUpdateDlg::DownloadFile(const char *url, const char *filename)
{
// Size of HTTP Buffer...
#define HTTPBUFLEN 512
char httpbuff[HTTPBUFLEN];
TCHAR szCause[255];
int nFileSize = 0;
CString Cause;
Cause.Format("YES");

TRY
{
CInternetSession mysession;
CStdioFile *remotefile = mysession.OpenURL(url,1,INTERNET_FLAG_TRANSFER_BINARY|INTERNET_FLAG_RELOAD);

DWORD dwFileSize;
char szSizeBuffer[32];
DWORD dwLengthSizeBuffer = 32;
HINTERNET hFileInfo;

hFileInfo = InternetOpenUrl(mysession, url, NULL, 0, 0, 0);

BOOL bQuery = ::HttpQueryInfo(hFileInfo,HTTP_QUERY_CONTENT_LENGTH, szSizeBuffer, &dwLengthSizeBuffer, NULL) ;

if(bQuery)
{
dwFileSize = atol(szSizeBuffer);
::InternetCloseHandle(hFileInfo);

//store the file size in an int variables,
//and divide it by 1024 to convert it to KB.
nFileSize = dwFileSize / 1024;

//set the range of progress bar to the total file size
m_DownloadStatus.SetRange32(0,nFileSize);
}
else
{
MessageBox("Unable to retrieve file sizes!","Error",MB_OK | MB_ICONWARNING);
}

CFile myfile(filename, CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
int numbytes = 0, nBytesTransfered = 0;

while(numbytes = remotefile->Read(httpbuff, HTTPBUFLEN))
{
//set the position accordingly in the progress bar
m_DownloadStatus.SetPos(nBytesTransfered / 1024);
}
}

CATCH_ALL(error)
{
error->GetErrorMessage(szCause,254,NULL);
Cause.Format("%s",szCause);
}
END_CATCH_ALL;

return (Cause);
}

UINT CUpdateDlg::_DownloadThread(LPVOID pParam)
{
//Convert from the SDK world to the C++ world
CUpdateDlg* pDlg = (CUpdateDlg*) pParam;
ASSERT(pDlg);
ASSERT(pDlg->IsKindOf(RUNTIME_CLASS(CUpdateDlg)));
pDlg->DownloadFile("http://www.geocities.com/xeon_boy/Dinos.zip","C:\\Windows\\Desktop\\ght.zip");
return 0;
}

void CUpdateDlg::OnDownload()
{
// TODO: Add your control notification handler code here

m_pThread = AfxBeginThread(_DownloadThread, this, THREAD_PRIORITY_NORMAL, CREATE_SUSPENDED);
m_pThread->m_bAutoDelete = FALSE;
m_pThread->ResumeThread();
}

void CUpdateDlg::OnCancel()
{
PostMessage(WM_HTTPDOWNLOAD_THREAD_FINISHED);

CDialog::OnCancel();
}

void CUpdateDlg::OnDestroy()
{
CDialog::OnDestroy();

// TODO: Add your message handler code here

if (m_pThread)
{
WaitForSingleObject(m_pThread->m_hThread, INFINITE);
delete m_pThread;
m_pThread = NULL;
}
}

void CUpdateDlg::OnClose()
{
// TODO: Add your message handler code here and/or call default

if(m_bSafeToClose)
{
CDialog::OnClose();
}
}


The thing is that when I run the program and the downloading process is half-way through
and I click the Cancel button(IDCANCEL , OnCancel), the modal dialog(CUpdateDlg) closes down
and everything is fine, but the main program HANGS and everything gets stucks! And I've to use
Alt + Ctrl + Del to close down the thing! Can you please help me? I've been trying this fro around 2 weeks and 3 days,
but just couldn't find the bug even after days and days of re-coding and testing and building and testing again.

Also, if you wish, I can send you the program for you to inspect, just give me your e-mail address.

Please help!!! Thanks a lot, really......in advance!

Good day,
Xeon
GeneralRe: A bug concerning 2 threads in my program Pin
Joaquín M López Muñoz22-Nov-01 22:21
Joaquín M López Muñoz22-Nov-01 22:21 
GeneralHandle of one window. Pin
22-Nov-01 21:01
suss22-Nov-01 21:01 
GeneralRe: Handle of one window. Pin
Nish Nishant22-Nov-01 21:26
sitebuilderNish Nishant22-Nov-01 21:26 
GeneralRe: Handle of one window. Pin
25-Nov-01 21:15
suss25-Nov-01 21:15 
GeneralCustom control for displaying status Pin
Christopher Lord22-Nov-01 18:32
Christopher Lord22-Nov-01 18:32 
GeneralRe: Custom control for displaying status Pin
Michael Dunn22-Nov-01 19:47
sitebuilderMichael Dunn22-Nov-01 19:47 
GeneralRe: Is CSocket "secure"? Pin
Nish Nishant22-Nov-01 19:08
sitebuilderNish Nishant22-Nov-01 19:08 
GeneralRe: Is CSocket "secure"? Pin
Rashid Thadha22-Nov-01 22:34
Rashid Thadha22-Nov-01 22:34 
GeneralText Processing Pin
Nick Parker22-Nov-01 18:07
protectorNick Parker22-Nov-01 18:07 
GeneralRe: Text Processing Pin
Michael Dunn22-Nov-01 19:50
sitebuilderMichael Dunn22-Nov-01 19:50 
GeneralRe: Text Processing Pin
Nick Parker23-Nov-01 4:09
protectorNick Parker23-Nov-01 4:09 
GeneralRe: Text Processing Pin
Michael Dunn23-Nov-01 8:14
sitebuilderMichael Dunn23-Nov-01 8:14 
GeneralRe: Text Processing Pin
Nick Parker23-Nov-01 10:57
protectorNick Parker23-Nov-01 10:57 
GeneralRe: Text Processing Pin
Michael Dunn23-Nov-01 11:17
sitebuilderMichael Dunn23-Nov-01 11:17 
GeneralRe: Text Processing Pin
Nick Parker24-Nov-01 1:54
protectorNick Parker24-Nov-01 1:54 
GeneralEnumPrinters function Pin
Brett G.22-Nov-01 16:49
Brett G.22-Nov-01 16:49 
QuestionHow to display a bitmap of 24 bits correctly?? Pin
Leesen22-Nov-01 16:23
Leesen22-Nov-01 16:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.