Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Application crashes on calling settimer from a thread function

Application crashes on calling settimer from a thread function

Scheduled Pinned Locked Moved C / C++ / MFC
designtutorialannouncement
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    manoharbalu
    wrote on last edited by
    #1

    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); }

    R V 2 Replies Last reply
    0
    • M manoharbalu

      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); }

      R Offline
      R Offline
      Roger Stoltz
      wrote on last edited by
      #2

      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 of CWnd - If it was not out-commented, AfxGetMainWnd() would return NULL

      "It's supposed to be hard, otherwise anybody could do it!" - selfquote
      "High speed never compensates for wrong direction!" - unknown

      1 Reply Last reply
      0
      • M manoharbalu

        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); }

        V Offline
        V Offline
        vikas amin
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups