I want to run Thread at regular interval using multimedia timer [modified]
-
I want to do two to three tasks at the same time at regular interval ,time is important so i m using multimedia timer . problem is ....timer run the thread only once .code is below when I click button thread is created and multimedia timer is set. count is an integer initialized with 0 void CTimerDlg::Onbtnstart() { Thread=AfxBeginThread(ThreadDisplay, this, THREAD_PRIORITY_TIME_CRITICAL,0 ,CREATE_SUSPENDED); handle=Thread->m_hThread; Thread->m_bAutoDelete=false; timeSetEvent(100, 0, TimeProcVideo, (DWORD)this, TIME_PERIODIC); } //thread analyzing data UINT ThreadDisplay(LPVOID lParam) { CTimerDlg *obj=(CTimerDlg*) lParam; obj->abc(); return 1; } void CTimerDlg::abc() { count++; char a[20]; GetDlgItem(IDC_STATIC)->SetWindowText(""); CString s=itoa(count,a,10); GetDlgItem(IDC_STATIC)->SetWindowText(s); SuspendThread(handle); } void CALLBACK TimeProcVideo(UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2) { CTimerDlg* timer=(CTimerDlg*) dwUser; timer->ControlThreads(); } void CTimerDlg::ControlThreads() { ResumeThread(handle); }
modified on Friday, November 28, 2008 11:18 PM
-
I want to do two to three tasks at the same time at regular interval ,time is important so i m using multimedia timer . problem is ....timer run the thread only once .code is below when I click button thread is created and multimedia timer is set. count is an integer initialized with 0 void CTimerDlg::Onbtnstart() { Thread=AfxBeginThread(ThreadDisplay, this, THREAD_PRIORITY_TIME_CRITICAL,0 ,CREATE_SUSPENDED); handle=Thread->m_hThread; Thread->m_bAutoDelete=false; timeSetEvent(100, 0, TimeProcVideo, (DWORD)this, TIME_PERIODIC); } //thread analyzing data UINT ThreadDisplay(LPVOID lParam) { CTimerDlg *obj=(CTimerDlg*) lParam; obj->abc(); return 1; } void CTimerDlg::abc() { count++; char a[20]; GetDlgItem(IDC_STATIC)->SetWindowText(""); CString s=itoa(count,a,10); GetDlgItem(IDC_STATIC)->SetWindowText(s); SuspendThread(handle); } void CALLBACK TimeProcVideo(UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2) { CTimerDlg* timer=(CTimerDlg*) dwUser; timer->ControlThreads(); } void CTimerDlg::ControlThreads() { ResumeThread(handle); }
modified on Friday, November 28, 2008 11:18 PM
:confused: :omg: :confused: What on earth are you trying to do? I mean that question very sincerely because you seem to have misunderstood the concept of multithreading completely. If you experience the thread to run at least once, you must be calling
::ResumeThread()
from some code that you have not provided in this post, which suggests that there may be more to it than the code you've posted. What the code you provided does is create a thread suspended. When the thread resumes execution it will call theabc()
function. In the same call chain::SuspendThread()
will be called and when::ResumeThread()
is called in order to let the thread resume execution, the thread will simply exit. That's why the "thread only runs once". For the rest of us to be able to help you as constructive as possible, please answer this: Why do you think you need multiple threads?"It's supposed to be hard, otherwise anybody could do it!" - selfquote
"High speed never compensates for wrong direction!" - unknown