Click to See Complete Forum and Search --> : Thread Problem


slv
November 8th, 2004, 01:08 PM
hi. i am having a problem with a thread and i hope you can help me solve it. my problem is that the param that i pass to the function that performs the thread seem to have benn lost on the way. the thing is that when i try to print them they are NULL. The code for the class is:
Download.h
#pragma once
typedef struct ThreadInfo {
CString strFile;
CString strUrl;
}ThreadInfo;

class Download {
public:
Download(void);
~Download(void);
Download(const char* Url , const char* File);
protected:
ThreadInfo ti;
public:
UINT StartThread(void);
static UINT DownloadFile(LPVOID pParam);
};
And the Download.cpp is:
#include "StdAfx.h"
#include ".\download.h"
Download::Download(void) {
}
Download::~Download(void) {
}
Download::Download(const char* Url , const char* File) {
ti.strFile = Url;
ti.strUrl = File;
}
UINT Download::StartThread(void) {
AfxMessageBox(ti.strFile);
if ( AfxBeginThread(DownloadFile, this))
return 0;
else
return 1;
}
UINT Download::DownloadFile(LPVOID pParam) {
Download * file = reinterpret_cast<Download*> (pParam); AfxMessageBox("yes!");
AfxMessageBox(file->ti.strUrl);
return 1;
}
And to use it :
Download file("http://slv.com", "slv"); file.StartThread();
i looked all-over 4 an answer but i colud not find one. I've seen exaples, they look the same, they work, but mine doesn't.

gstercken
November 8th, 2004, 01:13 PM
Please reformat your code to include line breaks. :)

Zim327
November 8th, 2004, 01:19 PM
first of all,
your code is difficult to read, can you put carriage returns in?
second, I surmise that you are passing a pointer to something when you call your worker thread and its null in the thread
well
inside your thread function you have to cast the LPVOID param back to the data type (a pointer to ThreadInfo perhaps?) you are looking for and then it should work...

gstercken
November 8th, 2004, 01:26 PM
hi. i am having a problem with a thread and i hope you can help me solve it. my problem is that the param that i pass to the function that performs the thread seem to have benn lost on the way. the thing is that when i try to print them they are NULL.Your problem is that you are creating a Download instance on the stack. Then you call StartThread(), which will call AfxBeginThread. However, after that, your main thread proceeds, the Download instance goes out of scope and is destroyed, and the pointer you have formerly passed is no longer valid.

slv
November 8th, 2004, 01:38 PM
thanx 4 the reply.
any ideas on how can i solve that? i want my 2nd thread to run at the beginning of the application, i do not want it to be button-triggered (or something-else)

gstercken
November 8th, 2004, 01:41 PM
thanx 4 the reply.
any ideas on how can i solve that? i want my 2nd thread to run at the beginning of the application, i do not want it to be button-triggered (or something-else)You just need to make sure that your Download object's lifetime is as least that of your thread. One way would be to create it as a member of your application class (instead of creating it locally on the stack).

slv
November 8th, 2004, 02:17 PM
thanx very much, it works just fine!!!!!

Fozi
November 8th, 2004, 04:31 PM
Here's an alternative where you don't have to care for the lifetime:

First, why do you pass a pointer to the Download class instead a pointer to a ThreadInfo to the thread function?

Second, if you make a copy of the ThreadInfo struct that you reserved with new, and delete it at the end of the thread you are not dependent from the starting object's lifetime any more.

Yo,

Fozi

Andreas Masur
November 9th, 2004, 04:03 AM
[ Moved thread ]