Click to See Complete Forum and Search --> : createthread gives me problem


doxdici
February 24th, 2006, 10:04 AM
hi,
i have a problem with CreateThread.
The first time I create a thread, all is ok.
But the second, the thread is created, but it takes control of the program,
so the MAIN does not take control.
I am sure to have well explained my problem.
Thank you, Isabella

g_gili
February 24th, 2006, 10:10 AM
No you didn't explained very well. :)
"the thread is created, but it takes control of the program"
Can you explain what do you mean ???

doxdici
February 24th, 2006, 10:21 AM
My program have two button, Start button and Stop button.
With Start button, starts a thread called Show.
The first time I press Start button, the thread starts and do all its homework, I can press the Stop button and the thread stops.
If I press the Start button another time, the thread starts and do all its homework, but I can't press the Stop button, so it seems that the thread take the "control"....
I hope this is more clear.
The instruction of CreateThread is the sequent:

hThread=CreateThread(NULL,0,
(LPTHREAD_START_ROUTINE)&Show,
this, 0,&idThread);


thanks,
Isabella

philkr
February 24th, 2006, 10:38 AM
Do you use a wait function? In this case the program waits until the thread finishes.
Another problem might be that the thread uses too much cpu time. Did you change any priority settings?

doxdici
February 24th, 2006, 10:53 AM
how can i do to change priority settings?

VictorN
February 24th, 2006, 10:55 AM
Isabella, post the code how the thread is started and the code of the thread procedure.
Don't forget to use Code tags!

doxdici
February 24th, 2006, 11:15 AM
hThread=CreateThread(NULL,0,
(LPTHREAD_START_ROUTINE)&Show,
this, // argument to thread function
0,//CREATE_SUSPENDED, // use default creation flags
&idThread);
SetThreadPriority(hThread,THREAD_PRIORITY_LOWEST);

if(!hThread)
{
AfxMessageBox("Thread not created");
return;
}


/////////////////////////////////////////////////////
////////////////////////////////////
DWORD WINAPI Show()
{
//char * msgw=new char[5];
// strcpy(msgw,"s\r");
// WriteCom(msgw);
//delete [] msgw;
if (seconda++>0)
return 5;
int Number=640*480*3;
BYTE* pdata=new BYTE[Number];
while (stop!=1)
{
if (stop==1)
{
delete [] pdata;
EndShow();
return 1;
}
if(!camera->OnStartVideo(pdata,Number))
return 0;
if (start==0)
{
start=GetTickCount();
//FILE* pFile=fopen("pippo2.txt","a");
//char* buf=new char[10];
//sprintf(buf,"time elapsed %i msec",start);
//fputs(buf,pFile);
//fclose(pFile);
}
else
{
time_elapsed=GetTickCount()-start;
start=0;
FILE* pFile=fopen("pippo2.txt","at");
char* buf=new char[50];
sprintf(buf,"time elapsed %i msec\n",time_elapsed);
fputs(buf,pFile);
delete [] buf;
fclose(pFile);
}
BITMAPINFO bmi;
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = 640;
bmi.bmiHeader.biHeight = 480;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 24;
bmi.bmiHeader.biCompression = BI_RGB;
bmi.bmiHeader.biSizeImage = 0;
bmi.bmiHeader.biXPelsPerMeter = 1000;
bmi.bmiHeader.biYPelsPerMeter = 1000;
bmi.bmiHeader.biClrUsed = 0;
bmi.bmiHeader.biClrImportant = 0;


if (stop==1)
{
delete [] pdata;
EndShow();
return 2;
}
CFrameWnd* pWnd = (CFrameWnd *) theApp.GetMainWnd();
RECT rect;
HDC hDC = ::GetDC(pWnd->m_hWnd);
pWnd->GetWindowRect(&rect);
::SetDIBitsToDevice(hDC, 0, 0, 640, 480, 0, 0, 0, 480, pdata, &bmi, DIB_RGB_COLORS);
::ReleaseDC(pWnd->m_hWnd,hDC);
if (stop==1)
{
delete [] pdata;
EndShow();
return 3;
}
}
delete [] pdata;
EndShow();
return 4;
}


BOOL EndShow()
{
char * msgw=new char[5];
strcpy(msgw,"b");
if (!WriteCom(msgw))
AfxMessageBox("Error Write");
delete [] msgw;
taking=0;
stop=0;
return TRUE;
}

doxdici
February 24th, 2006, 11:26 AM
hThread=CreateThread(NULL,0,
(LPTHREAD_START_ROUTINE)&Show,
this, // argument to thread function
0,//CREATE_SUSPENDED, // use default creation flags
&idThread);
SetThreadPriority(hThread,THREAD_PRIORITY_LOWEST);

if(!hThread)
{
AfxMessageBox("Thread not created");
return;
}


/////////////////////////////////////////////////////
////////////////////////////////////
DWORD WINAPI Show()
{


int Number=640*480*3;
BYTE* pdata=new BYTE[Number];
while (stop!=1)
{
if (stop==1)
{
delete [] pdata;
EndShow();
return 1;
}
if(!camera->OnStartVideo(pdata,Number))
return 0;
if (start==0)
{
start=GetTickCount();
//FILE* pFile=fopen("pippo2.txt","a");
//char* buf=new char[10];
//sprintf(buf,"time elapsed %i msec",start);
//fputs(buf,pFile);
//fclose(pFile);
}
else
{
time_elapsed=GetTickCount()-start;
start=0;
FILE* pFile=fopen("pippo2.txt","at");
char* buf=new char[50];
sprintf(buf,"time elapsed %i msec\n",time_elapsed);
fputs(buf,pFile);
delete [] buf;
fclose(pFile);
}
BITMAPINFO bmi;
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = 640;
bmi.bmiHeader.biHeight = 480;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 24;
bmi.bmiHeader.biCompression = BI_RGB;
bmi.bmiHeader.biSizeImage = 0;
bmi.bmiHeader.biXPelsPerMeter = 1000;
bmi.bmiHeader.biYPelsPerMeter = 1000;
bmi.bmiHeader.biClrUsed = 0;
bmi.bmiHeader.biClrImportant = 0;


if (stop==1)
{
delete [] pdata;
EndShow();
return 2;
}
CFrameWnd* pWnd = (CFrameWnd *) theApp.GetMainWnd();
RECT rect;
HDC hDC = ::GetDC(pWnd->m_hWnd);
pWnd->GetWindowRect(&rect);
::SetDIBitsToDevice(hDC, 0, 0, 640, 480, 0, 0, 0, 480, pdata, &bmi, DIB_RGB_COLORS);
::ReleaseDC(pWnd->m_hWnd,hDC);
if (stop==1)
{
delete [] pdata;
EndShow();
return 3;
}
}
delete [] pdata;
EndShow();
return 4;
}


BOOL EndShow()
{
char * msgw=new char[5];
strcpy(msgw,"b");
if (!WriteCom(msgw))
AfxMessageBox("Error Write");
delete [] msgw;
taking=0;
stop=0;
return TRUE;
}

doxdici
February 24th, 2006, 11:42 AM
this problem persists even if in the thread I do nothing, so the problem is on the calling of the thread function, I think

VictorN
February 24th, 2006, 11:49 AM
This part of your thread procedure is wrong: CFrameWnd* pWnd = (CFrameWnd *) theApp.GetMainWnd();
RECT rect;
HDC hDC = ::GetDC(pWnd->m_hWnd);
pWnd->GetWindowRect(&rect);
::SetDIBitsToDevice(hDC, 0, 0, 640, 480, 0, 0, 0, 480, pdata, &bmi, DIB_RGB_COLORS);
::ReleaseDC(pWnd->m_hWnd,hDC);
You should not directly access any window not belonging to the calling thread.
Besides, you must not access other thread's MFC objects (it can access only MFC objects that it created!).
Moreover, (from MSDN):If you have a multithreaded application that creates a thread in a way other than using aCWinThread object, you cannot access other MFC objects from that thread. In other words, if you want to access any MFC object from a secondary thread, you must create that thread with one of the methods described in the Multithreading: Creating User-Interface Threads or Multithreading: Creating Worker Threads articles. These methods are the only ones that allow the class library to initialize the internal variables necessary to handle multithreaded applications.
.....So, you should create your thread using AfxBeginThread method!

Also, take a look at this Joe Newcomer's essay: Worker Threads (http://www.flounder.com/workerthreads.htm)

krmed
February 24th, 2006, 11:56 AM
In your thread (Show) you have a while loop

while (stop!=1)
{
if (stop==1)
{
delete [] pdata;
EndShow();
return 1;
}
if(!camera->OnStartVideo(pdata,Number))
return 0;
if (start==0)
{
start=GetTickCount();
//FILE* pFile=fopen("pippo2.txt","a");
//char* buf=new char[10];
//sprintf(buf,"time elapsed %i msec",start);
//fputs(buf,pFile);
//fclose(pFile);
}
else
{
time_elapsed=GetTickCount()-start;
start=0;
FILE* pFile=fopen("pippo2.txt","at");
char* buf=new char[50];
sprintf(buf,"time elapsed %i msec\n",time_elapsed);
fputs(buf,pFile);
delete [] buf;
fclose(pFile);
}
BITMAPINFO bmi;
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = 640;
bmi.bmiHeader.biHeight = 480;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 24;
bmi.bmiHeader.biCompression = BI_RGB;
bmi.bmiHeader.biSizeImage = 0;
bmi.bmiHeader.biXPelsPerMeter = 1000;
bmi.bmiHeader.biYPelsPerMeter = 1000;
bmi.bmiHeader.biClrUsed = 0;
bmi.bmiHeader.biClrImportant = 0;


if (stop==1)
{
delete [] pdata;
EndShow();
return 2;
}
CFrameWnd* pWnd = (CFrameWnd *) theApp.GetMainWnd();
RECT rect;
HDC hDC = ::GetDC(pWnd->m_hWnd);
pWnd->GetWindowRect(&rect);
::SetDIBitsToDevice(hDC, 0, 0, 640, 480, 0, 0, 0, 480, pdata, &bmi, DIB_RGB_COLORS);
::ReleaseDC(pWnd->m_hWnd,hDC);
if (stop==1)
{
delete [] pdata;
EndShow();
return 3;
}
}

but when you hit the stop button (I'm guessing this calls the EndShow() function) you set

stop = 0;

If this is true, the thread never leaves the while loop! Therefore, the thread never ends. If you want the thread to end, your stop button needs to set

stop = -1;


That might be your problem.

Marc G
February 24th, 2006, 01:16 PM
[ moved thread ]

MikeAThon
February 24th, 2006, 05:36 PM
Please post all the code in the button handlers for your Start and Stop buttons.

In addition, if you have written minimal code that still exhibits your problem, it would be good to see that code instead of the actual code from your project. For example, you mentioned that if you delete all the work from the inside of your worker thread, you still see the problem. That would be better code for us to help you on, since it's not encumbered by a lot of unnecessary details.

Mike