Note that there are some explanatory texts on larger screens.

plurals
  1. POProblem in suspending 2 threads at the same time in MFC!
    primarykey
    data
    text
    <p>I am learning about threading and multithreading..so i just created a small application in which i will update the progressbar and a static text using threading.I vl get two inputs from the user, start and end values for how long the loop should rotate.I have 2threads in my application.</p> <p>Thread1- to update the progressbar(according to the loop) the static text which will show the count(loop count). Thread2 - to update the another static text which will just diplay a name</p> <p>Basically if the user clicks start, the progressbar steps up and at the same time filecount and the name are displayed parallely. There's is another operation where if the user clicks pause it(thread) has to suspend until the user clicks resume. The problem is,the above will not work(will not suspend and resume) for both thread..but works for a singlw thread. Please check the code to get an idea and reply me what can done!</p> <p>on button click start</p> <pre><code>void CThreadingEx3Dlg::OnBnClickedStart() { m_ProgressBar.SetRange(start,end); myThread1 = AfxBeginThread((AFX_THREADPROC)MyThreadFunction1,this); myThread2 = AfxBeginThread((AFX_THREADPROC)MyThreadFunction2,this); } </code></pre> <p>thread1</p> <pre><code>UINT MyThreadFunction1(LPARAM lparam) { CThreadingEx3Dlg* pthis = (CThreadingEx3Dlg*)lparam; for(int intvalue =pthis-&gt;start;intvalue&lt;=pthis-&gt;end; ++intvalue) { pthis-&gt;SendMessage(WM_MY_THREAD_MESSAGE1,intvalue); } return 0; } </code></pre> <p>thread1 function</p> <pre><code>LRESULT CThreadingEx3Dlg::OnThreadMessage1(WPARAM wparam,LPARAM lparam) { int nProgress= (int)wparam; m_ProgressBar.SetPos(nProgress); CString strStatus; strStatus.Format(L"Thread1:Processing item: %d", nProgress); m_Static.SetWindowText(strStatus); Sleep(100); return 0; } </code></pre> <p>thread2 </p> <pre><code>UINT MyThreadFunction2(LPARAM lparam) { CThreadingEx3Dlg* pthis = (CThreadingEx3Dlg*)lparam; for(int i =pthis-&gt;start;i&lt;=pthis-&gt;end;i++) { pthis-&gt;SendMessage(WM_MY_THREAD_MESSAGE2,i); } return 0; } </code></pre> <p>thread2 function</p> <pre><code>LRESULT CThreadingEx3Dlg::OnThreadMessage2(WPARAM wparam,LPARAM lparam) { m_Static1.GetDlgItem(IDC_STATIC6); m_Static1.SetWindowTextW(L"Thread2 Running"); Sleep(100); m_Static1.SetWindowTextW(L""); Sleep(100); return TRUE; } void CThreadingEx3Dlg::OnBnClickedPause() { // TODO: Add your control notification handler code here if(!m_Track) { m_Track = TRUE; GetDlgItem(IDCANCEL)-&gt;SetWindowTextW(L"Resume"); myThread1-&gt;SuspendThread(); WaitForSingleObject(myThread1-&gt;m_hThread,INFINITE); myThread2-&gt;SuspendThread(); m_Static.SetWindowTextW(L"Paused.."); } else { m_Track = FALSE; GetDlgItem(IDCANCEL)-&gt;SetWindowTextW(L"Pause"); myThread1-&gt;ResumeThread(); myThread2-&gt;ResumeThread(); /*myEventHandler.SetEvent(); WaitForSingleObject(myThread1-&gt;m_hThread,INFINITE);*/ } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload