|
The thread must finish execution on its own based on some condition. Use WaitForSingleObject or WaitForMultipleObjects if you got wait for some signal and u can also give time. There are other methods also that can be used to stop execution like signalling a flag etc.
|
|
|
|
|
Hi Dubey,
Thanx 4 ur reply.I'll try to implement it using waitformultipleobjects.
Sujan
|
|
|
|
|
|
Hi
Thanx for the link i'll look into it
Sujan
|
|
|
|
|
You could create a shutdown event to stop the worker thread execution.
Here's an example how to do it:
int CYourThreadClass::ThreadFunction()
{
while(1)
{
HANDLE h[2];
h[0] = m_eventShutdown.m_hObject;
h[1] = another handle;
DWORD dwRet = ::WaitForMultipleObjects(2,h,FALSE,YOURTIMEOUT)
if(dwRet==WAIT_OBJECT_0)
return 0;
else if(dwRet==WAIT_OBJECT_1)
else if(dwRet==WAIT_TIMEOUT)
else if(dwRet==WAIT_FAILED)
}
}
|
|
|
|
|
Hi Markowski
Thanx a lot. I am working with it.
Sujan
|
|
|
|
|
Hi Andrzej
One silly question. Sorry. How to manage with WAIT_OBJECT_1.Instead shall i use the created thread handle. I used it and it is always getting into that "Wait Timeout" state. OR have to create another event.
Thanx in advance
Sujan
|
|
|
|
|
Here's an example which manages with WAIT_OBJECT_1 . The example demonstates how to print to the debug output every 1 s:
int CYourThreadClass::ThreadFunction()
{
while(1)
{
int nCounter = 0;
HANDLE h[2];
h[0] = m_eventShutdown.m_hObject;
h[1] = m_eventPrintToDebug.m_hObject;
DWORD dwRet = ::WaitForMultipleObjects(2,h,FALSE,INFINITE)
if(dwRet==WAIT_OBJECT_0)
return 0;
else if(dwRet==WAIT_OBJECT_1)
TRACE("Counter = %d\n",n_Counter++);
else if(dwRet==WAIT_TIMEOUT)
else if(dwRet==WAIT_FAILED)
}
}
|
|
|
|
|
Hi Andrzej,
Thanx A Lot. I am working with it.
Actually i creating a number of threads using AfxBeginThread(..) function and trying to end them.
The code goes like this
CWinThread *multthreads[MAX_CONNECTIONS];
int count;
UINT DataAccessThreading(LPVOID pParam)
{
CoInitialize(NULL);
int* ind= (int *)pParam;
int index=*ind;
free(ind);
while(TRUE)
{
HANDLE h[2];
h[0] = m_eventShutdown.m_hObject;
h[1] = multthreads[count]->m_hThread;
DWORD dwRets = (DWORD) multthreads[count]->m_hThread;
DWORD dwRet = ::WaitForMultipleObjects(2,h,FALSE,INFINITE);
if(dwRet==WAIT_OBJECT_0)
{
TRACE("THREAD EXIT SUCCESSFULL-Not Killing \n");
return 0; //exit thread
}
else if(dwRet== (DWORD) multthreads[count]->m_hThread)//handle your synchronization object
{
test.CacheRefresh(index);
Sleep(refreshInterval); //Added by Sujan
}
else if(dwRet==WAIT_TIMEOUT)//handle timeout
{
TRACE("Wait Timeout \n");
}
else if(dwRet==WAIT_FAILED)// handle error
{
TRACE("Wait Failed \n");
}
}
return 0;
}
and the OnInitDialog i am creating a thread by using
int *p=(int *)malloc(sizeof(int));
*p=i;
multthreads[i]=AfxBeginThread(DataAccessThreading,p,0,0,NULL);
Am i going on the right way. Please comment on this.
While using this code i am not getting my event called at all.
SORRY FOR DISTURBING YOU.
AGAIN Thanx A Lot
Sujan
|
|
|
|
|
Hi Andrzej,
If you have got the problem with my code. I am waiting for your responce.
SORRY 4 DISTRUBING U Much
Sujan
|
|
|
|
|
Does this article help?
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
|
|
|
|
|
Yes Yes
Thanx David. Now i am working with it.
Sujan
|
|
|
|
|
Ok this has to be obvious, but somehow I must be missing something somewhere...
I made a dialog and on it I placed a static control with a bitmap on it (in the resource editor). All well, but when I want to change the bitmap displayed on the control, the control vanishes(does not get redrawn)...
I tried to create a CStatic control on the stack, but that one never shows... What`s wrong with this code?
This is placed in the OnInitDialog of the dialog:
m_statTest.Create(_T(""), WS_CHILD|WS_VISIBLE|SS_BITMAP|SS_CENTERIMAGE, tmprect,this);
m_statTest.SetBitmap( ::LoadBitmap(NULL, MAKEINTRESOURCE(IDB_MYBITMAP)));
The m_statTest control is never displayed ( and yes, the tmprect is inside the CDialog frame)...
Another CStatic control that I created with the classwizard from a CStatic on the Dialog resource, also disapears when I call the SetBitmap on it...
My bitmap has 256 colors, might that be the prob??? (although it does display correct when I never use the setbimap on the control added in the resource editor)
Any help is greatly appreciated...
Davy
|
|
|
|
|
Use the following code
On the cpp file add the following.
CRect tmprect(100,100,300,300);
m_statTest.Create(_T(""), WS_CHILD|WS_VISIBLE|SS_BITMAP|SS_CENTERIMAGE, tmprect,this);
bm.LoadBitmap(IDB_BITMAP);
HBITMAP hBitmap = (HBITMAP)bm.GetSafeHandle();
m_statTest.SetBitmap(hBitmap);
on the header file create the instance of bm as
CBitmap bm;
This will work
|
|
|
|
|
To make your code working change the first parameter in function ::LoadBitmap from NULL to AfxGetApp()->m_hInstance .
|
|
|
|
|
Thanks Sujan and Andrzej. Both solutions seem to work..
Seems to me that the MSDN library which explains the SetBitmap function of the CStatic control should be updated...
I got that specific codepart for the control created on the stack from the MSDN lib.
Ah another thing, when using the code part:
m_statCtrl.SetBitmap(::LoadBitmap(....)); //m_statCtrl is CStatic control
don^t you loose memory? I mean the documentation on LoadBitmap says that one should call DeleteObject on handles created by LoadBitmap... On the other hand, MSDN suggests this code-snippet to set the bitmap on a CStatic... I don^t think the CStatic deletes the handle when it gets destroyed or changed, or does it?????
(another thing that should be updated in MSDN perhaps?)
Greetz,
Davy
|
|
|
|
|
You will not have memory leaks because the system automatically deletes handles when the process that created them terminates.
|
|
|
|
|
How to get out the progress status in DirectShow when I encode a media file for example ?
|
|
|
|
|
I need to be able to mointor accessed files accessed on windows like sysintenal file mointor how I can do this.
|
|
|
|
|
you can try using The FindFirstChangeNotification(easier) or SHChangeNotify function.
|
|
|
|
|
Hi All,
Can some one tell me the differences between semaphores,critical section and mutexes.When should they be used(i know these are used for synchronization between threads) and the advantages/disadvantages of them over the other.
Thanks in advance
Regards
Raghu
|
|
|
|
|
in one sentence.
Semaphore can be used to manage resources, criticalsection to synchronize access to shared data within same(single) process and mutex to synchronize access to shared data among different process also.
There is no spoon.
mail
|
|
|
|
|
Hello Friends,
I got a problem.
I had splitted the actual view into 4 parts. every view is been derived from different view's class.
The main problem is the one splitter window which has been derived from the FormView has got 2 edit boxes which will resizing itself based upon the size of the window. and when I click in someother view some message has to be displayed in these edit boxes. for example when I click in View1 then some message which I pass from View1 has to be displayed in the edit boxes present in the View2.
I am trying but its always giving me assertion failed and when I debug it its giving the asserting failed in the SetWindowText message. I had created 2 different variables of type CEdit.
Neelesh K J Jain.
I am getting the assertion failed in the winocc.cpp at line 229, When I am using the SetWindowText
Neelesh K J Jain.
|
|
|
|
|
Get the view address using GetActivePane() function and use it for setting the window text using setwindow text function
eg.
CMyView *pView = (CMyView *) mysplitter.GetActivePane();
pView->SetWindowText("Hi");
|
|
|
|
|
Its giving me a problem at the SetWindowText message in the Winocc.cpp file.
|
|
|
|