Click here to Skip to main content
15,890,845 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: URLDownloadToFile, Cancel Button Pin
enhzflep18-Nov-11 14:15
enhzflep18-Nov-11 14:15 
GeneralRe: URLDownloadToFile, Cancel Button Pin
jkirkerx18-Nov-11 17:05
professionaljkirkerx18-Nov-11 17:05 
AnswerRe: URLDownloadToFile, Cancel Button Pin
Chuck O'Toole18-Nov-11 17:25
Chuck O'Toole18-Nov-11 17:25 
GeneralRe: URLDownloadToFile, Cancel Button Pin
jkirkerx18-Nov-11 18:02
professionaljkirkerx18-Nov-11 18:02 
GeneralRe: URLDownloadToFile, Cancel Button Pin
enhzflep18-Nov-11 20:28
enhzflep18-Nov-11 20:28 
GeneralRe: URLDownloadToFile, Cancel Button Pin
jkirkerx19-Nov-11 7:15
professionaljkirkerx19-Nov-11 7:15 
GeneralRe: URLDownloadToFile, Cancel Button Pin
enhzflep19-Nov-11 15:00
enhzflep19-Nov-11 15:00 
GeneralRe: URLDownloadToFile, Cancel Button Pin
jkirkerx19-Nov-11 17:52
professionaljkirkerx19-Nov-11 17:52 
I just didn't want to sound like I wanted someone to fix my code because I posted it. No insult intended.

I got it to work this afternoon. I sort of did the same thing, but not as elaborate. I examined the code sample in CP, and put the if statement back in, and made it a long this time instead of a boolean.

I just needed to get the mechanics working, so I didn't go around in circles trying complex things. I cleaned up this program alot, and got rid of some the erratic side effects of experimenting. It's not perfect in the purest sense, but it's a start. Now I can clean it up some more based on feedback, and write a check to see if the file is already downloaded, and integrate the installation program I already wrote.

Edit:
After reading your post several times, I realize that all of the benefits you listed are well worth it. And you right about pushing the cancel to the thread. But having the byte count, and being able to resume is pretty sweet, when you need to pause to do something else, and pick up again. I wanted to write a socket based program, but I've only been doing this since oct 5, so I had to keep it simple for now. Are you checksuming your download?


I put the variable in the h file for the callback

SQL_Servers_BindCallback::SQL_Servers_BindCallback ( ) : m_Progress_Text(NULL), m_Progress_Bar(NULL) {
				
	}

	SQL_Servers_BindCallback::~SQL_Servers_BindCallback( ) {	
	
	}
			
	HRESULT SQL_Servers_BindCallback::OnProgress(
		/* [in] */ ULONG ulProgress, 
		/* [in] */ ULONG ulProgressMax, 
		/* [in] */ ULONG ulStatusCode, 
		/* [in] */ LPCWSTR wszStatusText)
	{				
		static int iMsgLen;
		static double dBytes;
		static double dTotal;
		static double dPercent;
		static double dPB;	
		static TCHAR szStatusMessage[80];

		if ( g_fAbortDownload == 0 ) <- put in h file of this cpp file
			return E_ABORT;


Then I made the callback global to the window page, and changed the way I close the thread.

C#
BOOL _download_SQLServer_Begin( BOOL bCancel ) {

    // lots of code ........

    int errorNumber;
    hThread = (HANDLE)_beginthreadex(NULL, 0, SQL_SoftwareDownload, NULL, 0, (unsigned *)&thread_id);
    if (hThread == 0) {
        errorNumber = errno;
    }
    Sleep(2000);
    int rv = WaitForSingleObjectEx(hThread, 3000, FALSE);
    if (rv == WAIT_OBJECT_0)
        resultCode = 1;

    return resultCode;
}

unsigned int __stdcall SQL_SoftwareDownload(void *) {
    int iReturn = 0;
    HRESULT hr = E_FAIL;

    // Globally declare the callback
    callback = new SQL_Servers_BindCallback;
    callback->m_MDIChild = hSQL_Servers_Download;
    callback->m_Progress_Text = lbl_SQL_Server_Download_Status;
    callback->m_Progress_Bar = pb_SQL_Server_Download_Status;

    hr = URLDownloadToFile(NULL, szUrl, szFileName, 0, callback);
    if (hr == S_OK)
        iReturn = 1;

    return iReturn;
}

BOOL _download_SQLServer_End( BOOL bCancel ) {
		
	BOOL			bResult								= FALSE;
	BOOL			bHandle								= FALSE;
	unsigned 		iThread;
		
	// Signal the Callback Loop
	callback->g_fAbortDownload = 0; < - made the call here, callback picked it up and aborted
	callback->Release();

	SetWindowText(lbl_SQL_Server_Download_Status, L"Disconnecting from download.microsoft.com");
	UpdateWindow(hSQL_Servers_Download);
	
	iThread = WaitForSingleObjectEx(hThread, 3000, FALSE); <- I thought this was needed
	bHandle = CloseHandle(hThread); < - I know _endthread is called automatically
	if (bHandle == 0) <- Not sure, but it shuts down real smooth like.
		Sleep(2000);			
		
	ShowWindow(pb_SQL_Server_Download_Status, SW_HIDE);
	ShowWindow(bt_SQL_Server_Download_Cancel, SW_HIDE);
	ShowWindow(bt_SQL_Server_Download_Exit, SW_SHOW);	

GeneralRe: URLDownloadToFile, Cancel Button Pin
enhzflep20-Nov-11 0:56
enhzflep20-Nov-11 0:56 
GeneralRe: URLDownloadToFile, Cancel Button Pin
jkirkerx20-Nov-11 18:06
professionaljkirkerx20-Nov-11 18:06 
QuestionProblem with MSDN Preview Handler Recipe Pin
AndrewG123115-Nov-11 13:26
AndrewG123115-Nov-11 13:26 
QuestionOT - question for real COM / RS232 guru Pin
Vaclav_15-Nov-11 10:33
Vaclav_15-Nov-11 10:33 
AnswerRe: OT - question for real COM / RS232 guru Pin
Richard MacCutchan15-Nov-11 22:00
mveRichard MacCutchan15-Nov-11 22:00 
GeneralRe: OT - question for real COM / RS232 guru Pin
Vaclav_16-Nov-11 2:50
Vaclav_16-Nov-11 2:50 
GeneralRe: OT - question for real COM / RS232 guru Pin
Richard MacCutchan16-Nov-11 3:19
mveRichard MacCutchan16-Nov-11 3:19 
RantSmart pointers Pin
Pascal Ganaye15-Nov-11 6:15
Pascal Ganaye15-Nov-11 6:15 
GeneralRe: Smart pointers Pin
Erudite_Eric15-Nov-11 7:33
Erudite_Eric15-Nov-11 7:33 
GeneralRe: Smart pointers Pin
JackDingler15-Nov-11 11:23
JackDingler15-Nov-11 11:23 
GeneralRe: Smart pointers Pin
Stefan_Lang17-Nov-11 4:42
Stefan_Lang17-Nov-11 4:42 
GeneralRe: Smart pointers Pin
JackDingler17-Nov-11 5:48
JackDingler17-Nov-11 5:48 
GeneralRe: Smart pointers Pin
Stefan_Lang17-Nov-11 5:56
Stefan_Lang17-Nov-11 5:56 
GeneralRe: Smart pointers Pin
Orjan Westin15-Nov-11 23:41
professionalOrjan Westin15-Nov-11 23:41 
GeneralRe: Smart pointers Pin
Erudite_Eric16-Nov-11 22:39
Erudite_Eric16-Nov-11 22:39 
GeneralRe: Smart pointers Pin
Orjan Westin21-Nov-11 4:33
professionalOrjan Westin21-Nov-11 4:33 
GeneralRe: Smart pointers Pin
Erudite_Eric21-Nov-11 5:59
Erudite_Eric21-Nov-11 5:59 

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.