Click here to Skip to main content
15,886,851 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
HI to all,
I am using CWinThread for streaming video from webcam remotely,so while creating and terminating thread I am getting errors(RunTime Error).It is creating successfully for first time,but when I am creating thread continuosly in timer after sometime I am suppose to get error and also while exiting my application I am getting errors the thread is not terminating properly.Can anyone help me out from this problem.Is there any other way instead of multithreading.If no alternative method can anyone help me out how to create and terminate thread properly.Thanks to all in advance.


Regards,
RamyaRaj
Posted

You need thread and you don't need alternative to thread, you need alternative to timer! Creating a thread by timer is a typical mistake, not very frequent though. Chances are, you need just one additional thread for whole run-time, only you should throttle it using even wait handle or some other synchronization primitive to keep your thread in wait state (spending zero CPU time for the wait) until it is given a job.

This is the idea. Unfortunately, I cannot give you more detail because you did not share your code or any relevant detail on your operation.

Don't even play with the idea of going without threads!
Get rid of your timer. Timers are evil.

—SA
 
Share this answer
 
Comments
RamyaRaj from NewDelhi 20-May-11 2:17am    
hi SAKryukov,

I used AfxBeginThread to create thread.

CWinThread* m_ThRecieveImage;
m_ThRecieveImage = AfxBeginThread(RecieveImage,m_CamNo,THREAD_PRIORITY_NORMAL,0,0,NULL);

I am using 16 cameras for each camera i am creating one like this.And in timer also i am calling this continuosly.

Can you please help me out from this problem.

Regards,
RamyaRaj.
Albert Holguin 20-May-11 10:59am    
when are you creating the threads? when the timer is called? are you loading all the threads on the same member pointer? if so, what's the use? you're overriding your reference to that thread.
Sergey Alexandrovich Kryukov 20-May-11 23:20pm    
Yes, good point.
And still no point to use a timer. Makes no sense.
--SA
RamyaRaj from NewDelhi 21-May-11 5:27am    
hi Sakryukov,
Now i removed timer in my code.I am creating thread for each camera is it anything wrong.

Regards,
RamyaRaj.
RamyaRaj from NewDelhi 21-May-11 5:34am    
hi Albert Holguin,
I created Pointer variable Seperately for all threads.I made a mistake of using timers,now i removed timer.But how to terminate thread and how to find wheather thread is terminated properly or not.While exiting my application i am suspending thread and terminating it but still its not terminating.Is there any other way to terminate thread.Here is the code what i used.

here m_iCamNo is camera number(I used 16 cameras).
if(m_ThRecieveImage[m_iCamNo]) m_ThRecieveImage[m_iCamNo]->SuspendThread();

::TerminateThread(m_ThRecieveImage[m_iCamNo],90);


Is there any thing wrong in that.


Thanks in advance.


Regards,
RamyaRaj.
You need to sit and go through a tutorial on multithreading and using AfxBeginThread. Post a bit of code and we'll give you some advice as to what you're doing wrong, but you really need to learn about how to properly multithread first.
 
Share this answer
 
Comments
RamyaRaj from NewDelhi 21-May-11 5:47am    
hi Albert Holguin, I created Pointer variable Seperately for all threads.I made a mistake of using timers,now i removed timer.But how to terminate thread and how to find wheather thread is terminated properly or not.While exiting my application i am suspending thread and terminating it but still its not terminating.Is there any other way to terminate thread.Here is the code what i used. here m_iCamNo is camera number(I used 16 cameras). if(m_ThRecieveImage[m_iCamNo]) m_ThRecieveImage[m_iCamNo]->SuspendThread(); ::TerminateThread(m_ThRecieveImage[m_iCamNo],90);
Is there any thing wrong in that. Thanks in advance.
Regards, RamyaRaj.
Albert Holguin 22-May-11 16:35pm    
that's not the proper way to terminate a thread, like I said, read a tutorial before you start using them
RamyaRaj from NewDelhi 23-May-11 0:35am    
Ok Albert Holguin,
To terminate thread can i use WaitForSingleObject function
Albert Holguin 24-May-11 14:17pm    
WaitForSingleObject just waits for a signal to occur, it can be used to wait for a worker thread to finish on its own... if you use a user interface thread based on CWinThread, then you typically ask the thread to stop what it's doing and exit (using a PostMessage(WM_QUIT)), then wait for it to finish with a WaitForSingleObject() on the thread handle.
RamyaRaj from NewDelhi 1-Jun-11 1:24am    
hi Albert Holguin,
If I use PostMessage(WM_QUIT) it will post that message to process or thread.If it process to thread then this PostMessage will send message to which thread ,because I am using 16 threads one for each camera.Can you please help me out.Thanks in Advance.

Regards,
RamyaRaj.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900