MFC主线程是否有状态判断?

新入职做一个小程序,功能不多描述了主要阐述一下问题。 在一个按钮下开启一个后台线程,然后通过SendMessage发送给界面的,响应函数为ListCo…
关注者
5
被浏览
689

3 个回答

1 数据多的话,不要用动态添加ListControl的方式,用虚列表,具体用法可以去参考Windows SDK的vlistvw示例。注意跨线程读写数据要加锁,避免一个没写完另一个就开始读。

2 界面线程不能长时间挂起。长时间的操作应该移动到后台线程。界面线程可以加个定时器看看后台线程是不是把数据写好了,然后去读取数据。在CEvent的文档( CEvent Class )里有介绍跨线程通讯的方法:

 CPrimeTest()
       : m_pCalcNext(new CEvent(FALSE, FALSE)),
         m_pCalcFinished(new CEvent(FALSE, FALSE)),
         m_pTerminateThread(new CEvent(FALSE, FALSE)),
         m_iCurrentPrime(0)
      // Create a thread that will calculate the prime numbers
      CWinThread *pThread;
      pThread = ::AfxBeginThread(&PrimeCalcProc,
                                 this, 0, 0, CREATE_SUSPENDED, NULL);
      pThread->m_bAutoDelete = FALSE;
      pThread->ResumeThread();
      // Calcuate the first 10 prime numbers in the series on the thread
      for (UINT i = 0; i < 10; i++)
         // Signal the thread to do the next work item
         m_pCalcNext->SetEvent();
         // Wait for the thread to complete the current task
         ::WaitForSingleObject(m_pCalcFinished->m_hObject, INFINITE);
         // Print the result
         TRACE(_T("The value of m_iCurrentPrime is: %d\n"), m_iCurrentPrime);