Subclassing a listcontrol in dialog bar
-
I have to display a Customized list control in a dialog bar and update it frequently. I am subclassing a custom list control in my application using: m_cLstCtrl.SubclassDlgItem(IDC_LSTCTRL_ALARM,this); CSysWindow is the dialog bar class and have created in CMainFrm using: if (!m_SysWnd.Create(this, IDD_DLGBAR_APPBAR, WS_CHILD | WS_VISIBLE | CBRS_GRIPPER |CBRS_FLYBY|CBRS_TOOLTIPS | CBRS_SIZE_DYNAMIC, IDD_DLGBAR_APPBAR)) { TRACE0("Failed to create DlgBar\n"); return -1; // fail to create } void CSysWindow::RecentAlarms() { //I am testing my List control using the below code: m_cLstCtrl.SetColumnHeader(_T("Student ID, 100; Enroll Date, 150; Score, 80, 2")); for (int i = 0; i < 10; i++) { const int IDX = m_cLstCtrl.InsertItem(0, _T("")); m_cLstCtrl.SetItemText(IDX, 0, ""); m_cLstCtrl.SetItemText(IDX, 1, "testDate"); m_cLstCtrl.SetItemText(IDX, 2, (rand() % 51) + 50); for (int j = 0; j < 2; j++) m_cLstCtrl.SetItemImage(IDX, j, rand() % 5); // subitem images } } When I call this function inside a thread, the application crashes by triggering a breakpoint. void tUpdateAllViews( CMainFrame *pMainFrm ) { while( 1 ) { pMainFrm->UpdateAllViews(); pMainFrm->m_SysWnd.RecentAlarms(); Sleep(500); } } void CMainFrame::UpdateAllViews() { //It also crashes when I call here as below. m_SysWnd.RecentAlarms(); } Whereas when I Call this function inside OnUpdate, its working void CMainFrame::OnUpdate() { // TODO: Add your command handler code here m_SysWnd.RecentAlarms(); } which is the proper way to call this function that is in the dialog bar class "m_SysWnd.RecentAlarms();" where to call this function? Please advice.
-
I have to display a Customized list control in a dialog bar and update it frequently. I am subclassing a custom list control in my application using: m_cLstCtrl.SubclassDlgItem(IDC_LSTCTRL_ALARM,this); CSysWindow is the dialog bar class and have created in CMainFrm using: if (!m_SysWnd.Create(this, IDD_DLGBAR_APPBAR, WS_CHILD | WS_VISIBLE | CBRS_GRIPPER |CBRS_FLYBY|CBRS_TOOLTIPS | CBRS_SIZE_DYNAMIC, IDD_DLGBAR_APPBAR)) { TRACE0("Failed to create DlgBar\n"); return -1; // fail to create } void CSysWindow::RecentAlarms() { //I am testing my List control using the below code: m_cLstCtrl.SetColumnHeader(_T("Student ID, 100; Enroll Date, 150; Score, 80, 2")); for (int i = 0; i < 10; i++) { const int IDX = m_cLstCtrl.InsertItem(0, _T("")); m_cLstCtrl.SetItemText(IDX, 0, ""); m_cLstCtrl.SetItemText(IDX, 1, "testDate"); m_cLstCtrl.SetItemText(IDX, 2, (rand() % 51) + 50); for (int j = 0; j < 2; j++) m_cLstCtrl.SetItemImage(IDX, j, rand() % 5); // subitem images } } When I call this function inside a thread, the application crashes by triggering a breakpoint. void tUpdateAllViews( CMainFrame *pMainFrm ) { while( 1 ) { pMainFrm->UpdateAllViews(); pMainFrm->m_SysWnd.RecentAlarms(); Sleep(500); } } void CMainFrame::UpdateAllViews() { //It also crashes when I call here as below. m_SysWnd.RecentAlarms(); } Whereas when I Call this function inside OnUpdate, its working void CMainFrame::OnUpdate() { // TODO: Add your command handler code here m_SysWnd.RecentAlarms(); } which is the proper way to call this function that is in the dialog bar class "m_SysWnd.RecentAlarms();" where to call this function? Please advice.
manoharbalu wrote:
When I call this function inside a thread, the application crashes by triggering a breakpoint.
You must not directly access the main GUI thread controls from within a secondary thread! Instead you should PostMessage a user defined message to the main thread window notifying it that it has to do some operation(s). Within the main thread you handle this message and perform the ordered operation(s). See also this great J.Newcomer essay: [Using Worker Threads](http://www.flounder.com/workerthreads.htm)