Application crashes on calling settimer from a thread function
-
I have created a dialog to display message on my application for 5 seconds after which the dialog closes. I created the dialog using the following code before which I update the display message text for the dialog sDispMsg = (CString)"Setting up the Design Condition"; CProgressDlg pProg; pProg.DoModal(); The above code is within a function. When I call the function it works fine. But when I call the above function after creating a thread using the code: if( bAteRunThreadActive == FALSE ) { htAteRunThread = CreateThread( NULL,0,(LPTHREAD_START_ROUTINE)tExecuteAte,(LPVOID)NULL,NULL,&dwAteRunThreadID ); if( dwAteRunThreadID == NULL ) { AfxMessageBox( "ATE Thread Creation Failed" ); return; } bAteRunThreadActive = TRUE; } Note that when I am creating the dialog inside the thread function, my application crashes at the point after I call the Settimer in the below code. When I call normally outside the thread in a normal function it works fine. Also when I did a step by step debugging it works fine in the thread function also. Pls provide some solution. //My Dialog code BOOL CProgressDlg::OnInitDialog() { CDialog::OnInitDialog(); // TODO: Add extra initialization here GetDlgItem(IDC_STATIC_DISPMSG)->SetWindowText(sDispMsg); tID = SetTimer(1,5000, NULL); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } void CProgressDlg::OnTimer(UINT nIDEvent) { // TODO: Add your message handler code here and/or call default /* int nRet = 5; */ KillTimer(tID); //EndDialog(0); CDialog::OnCancel(); // DestroyWindow(); // AfxGetMainWnd()->UpdateWindow(); //CDialog::OnTimer(nIDEvent); }
-
I have created a dialog to display message on my application for 5 seconds after which the dialog closes. I created the dialog using the following code before which I update the display message text for the dialog sDispMsg = (CString)"Setting up the Design Condition"; CProgressDlg pProg; pProg.DoModal(); The above code is within a function. When I call the function it works fine. But when I call the above function after creating a thread using the code: if( bAteRunThreadActive == FALSE ) { htAteRunThread = CreateThread( NULL,0,(LPTHREAD_START_ROUTINE)tExecuteAte,(LPVOID)NULL,NULL,&dwAteRunThreadID ); if( dwAteRunThreadID == NULL ) { AfxMessageBox( "ATE Thread Creation Failed" ); return; } bAteRunThreadActive = TRUE; } Note that when I am creating the dialog inside the thread function, my application crashes at the point after I call the Settimer in the below code. When I call normally outside the thread in a normal function it works fine. Also when I did a step by step debugging it works fine in the thread function also. Pls provide some solution. //My Dialog code BOOL CProgressDlg::OnInitDialog() { CDialog::OnInitDialog(); // TODO: Add extra initialization here GetDlgItem(IDC_STATIC_DISPMSG)->SetWindowText(sDispMsg); tID = SetTimer(1,5000, NULL); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } void CProgressDlg::OnTimer(UINT nIDEvent) { // TODO: Add your message handler code here and/or call default /* int nRet = 5; */ KillTimer(tID); //EndDialog(0); CDialog::OnCancel(); // DestroyWindow(); // AfxGetMainWnd()->UpdateWindow(); //CDialog::OnTimer(nIDEvent); }
Whow, you've managed to make a lot of conceptional errors. Perhaps you don't need a secondary thread at all. If you would create a modeless dialogue instead of a modal one, you would be able to continue doing what you should in one single thread. But if you think that you do need a secondary thread, you'd better read this[^] before you continue. In your case it's essential that you do read the article. Main issues are: - don't do GUI stuff from a worker thread -
SetTimer
is considered GUI stuff since it is a member ofCWnd
- If it was not out-commented,AfxGetMainWnd()
would returnNULL
"It's supposed to be hard, otherwise anybody could do it!" - selfquote
"High speed never compensates for wrong direction!" - unknown -
I have created a dialog to display message on my application for 5 seconds after which the dialog closes. I created the dialog using the following code before which I update the display message text for the dialog sDispMsg = (CString)"Setting up the Design Condition"; CProgressDlg pProg; pProg.DoModal(); The above code is within a function. When I call the function it works fine. But when I call the above function after creating a thread using the code: if( bAteRunThreadActive == FALSE ) { htAteRunThread = CreateThread( NULL,0,(LPTHREAD_START_ROUTINE)tExecuteAte,(LPVOID)NULL,NULL,&dwAteRunThreadID ); if( dwAteRunThreadID == NULL ) { AfxMessageBox( "ATE Thread Creation Failed" ); return; } bAteRunThreadActive = TRUE; } Note that when I am creating the dialog inside the thread function, my application crashes at the point after I call the Settimer in the below code. When I call normally outside the thread in a normal function it works fine. Also when I did a step by step debugging it works fine in the thread function also. Pls provide some solution. //My Dialog code BOOL CProgressDlg::OnInitDialog() { CDialog::OnInitDialog(); // TODO: Add extra initialization here GetDlgItem(IDC_STATIC_DISPMSG)->SetWindowText(sDispMsg); tID = SetTimer(1,5000, NULL); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } void CProgressDlg::OnTimer(UINT nIDEvent) { // TODO: Add your message handler code here and/or call default /* int nRet = 5; */ KillTimer(tID); //EndDialog(0); CDialog::OnCancel(); // DestroyWindow(); // AfxGetMainWnd()->UpdateWindow(); //CDialog::OnTimer(nIDEvent); }
you can use the thread.sleep and loops to get similar functionality http://msdn.microsoft.com/en-us/library/system.threading.thread.sleep(VS.71).aspx[^]
Vikas Amin
My First Article on CP" Virtual Serial Port "[^]
modified on Thursday, July 24, 2008 5:33 PM