Click here to Skip to main content
15,849,785 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, Good morning / afternoon / evening , Now i face a problem, i make a timer in my thread, it does not work at all..
but when i try to separate both of it.. it work normally,

here is the sample of my source code.

in .header

C++
static UINT MyThread(LPVOID pParam);
int a();
CWinThread* thread;
bool threadofmine;


in .cpp
C++
//this is the code i mean it does not work
void ____::OnClickedButton()
{
thread=AfxBeginThread(MyThread,this);
threadofmine=true;
}

UINT ____::MyThread(LPVOID pParam)
{
____* dialog;
dialog=(____*)pParam;
dialog->a();
return 0;
}

int  ____::a()
{
while(threadofmine)
{
SetTimer(1,1000,NULL);
group = 'a';
Sleep(100);
}
return 1;
}

void ____::OnTimer(UINT_PTR nIDEvent)
{
CDialogEx::OnTimer(nIDEvent);
switch(group)
{
case 'a':
{
timer+=1;
if(timer == 5)
{
MessageBox(NULL,NULL,NULL);
UpdateData(false);
}
break;
}


this is the code after i separate it,
C++
//for thread
void ___-:OnClickedButton()
{
thread=AfxBeginthread(MyThread,this);
threadofmine = true;
}

UINT ____::MyThread(LPVOID pParam)
{
____* dialog;
dialog=(____*)pParam;
dialog->a();
return 0;
}
 
int  ____::a()
{
while(threadofmine)
{
i+=1;
if(i==1)
{
MessageBox(L"1",NULL,NULL);
}
}return1;
}


this is for timer
C++
void ____::OnClickedButton()
{
SetTimer(1,1000,NULL);
group = 'a';
}

void ____::OnTimer(UINT_PTR nIDEvent)
{
CDialogEx::OnTimer(nIDEvent);
switch(group)
{
case 'a':
{
timer+=1;
if(timer == 5)
{
MessageBox(NULL,NULL,NULL);
UpdateData(false);
}
break;
}


please help me.. :"(
Posted
Updated 10-Oct-12 18:45pm
v3

SetTimer should be called from the GUIThread[thread which created the window]. In the error case, MyThread() is calling SetTimer and it will not work.

You have to call it from
C++
void ____::OnClickedButton()


or
you should post a user message to GUIThread, and call SetTimer from the handler of user message.
 
Share this answer
 
v2
Comments
xnold 11-Oct-12 0:00am    
thanks for the feedback.. :)
but the problem.. i have set multiple of timer in the case of 'group'
this is the example,

switch(group)
{
case 'a':
{
timer1+=1;
if(timer1==1)
{
KillTimer(1);
if(MessageBox(L"1",NULL,MB_OKCANCEL)==IDOK)
{
KillTimer(1);
SetTimer(2,1000,NULL);
timer='b';
}
UpdateData(false);
}
break;
}
case 'b':
{
timer2+=1;
if(timer2==1)
{
if(MessageBox(L"1",NULL,MB_OKCANCEL)==IDOK)
{
KillTimer(2);
// goto tryagain;
}
}
UpdateData(false);
}
xnold 11-Oct-12 0:00am    
i try ur method... it worked on the 1st timer, but the other timer did not work..
xnold 11-Oct-12 0:03am    
should i just change it to sleep in this kind of case??
but i worry bout the function of my code does not work due to it was in sleep mode
xnold 11-Oct-12 0:14am    
how to post user message to the thread?? can u please gime a simple of sample code.. thanks :)
Message declaration.
C++
const int SET_TIMER_MESSAGE = WM_USER + 1 // User Mesasge


message handler modification.
// Add message handler in message map.
ON_MESSAGE( SET_TIMER_MESSAGE, OnMySetTimerMessage )



// Message handler implementation.
C++
void MyDlgClass::OnMySetTimerMessage( WPARAM dwWParam_i, LPARAM dwLParam_i)
{
    // Process GUI actions. like SetTimer, UpdateData etc..
}


// Non-GUI thread sending message to GUI thread for gui related operations.
C++
UINT MyThread()
{
// Non gui actions 

...

// Send message to GUI for GUI actions
ptrDlgHandle->PostMessage( SET_TIMER_MESSAGE, 0,0 );
}
 
Share this answer
 
Comments
xnold 11-Oct-12 0:31am    
thx.. :)
xnold 18-Oct-12 1:22am    
hai.. sorry for bothering you,, i would like to ask,,, ptrDlgHandle u declare it as ??
Santhosh G_ 18-Oct-12 2:43am    
ptrDlgHandle is the handle to the window. It can be declared in global scope, and assign m_hWnd to it from OnInitDialog().
Or you can use findwindow to get the GUI from the thread.
xnold 18-Oct-12 20:41pm    
ic.. thx for ur feedback.. :)
Just remember this as a rule: GUI updates should be made from the GUI thread. If you really want to learn how to send messages from a thread to the GUI, please check this article[^].

You will see that there are user messages sent from the thread, and there are handlers defined in the dialog like this:
C++
ON_MESSAGE(WM_MY_COPY_PROGRESS, &CImageCataloguerDlg::OnCopyProgress)


I think you should also create a synchronisation object (like a event) to allow stopping the thread if required.

I would also replaced the Sleep function with a WaitForSingleObject function to check is the synchronisation object signalled.
 
Share this answer
 

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