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. Dialog 'hang' after C procedure called.

Dialog 'hang' after C procedure called.

Scheduled Pinned Locked Moved C / C++ / MFC
testingbeta-testinghelpquestionannouncement
8 Posts 5 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.
  • S Offline
    S Offline
    searcher08
    wrote on last edited by
    #1

    I have completed this dialog window reading in filenames and a progress bar to indicate progress. A timer is used to update the progress bar. However, when I called a C procedure that is computationally intensive, all the edit boxes and menu went blank. The worst thing was that the progress bar was not incrementing. Do take note that the program is running fine except for these side effects. The processed output was tested to be correct. Before I include the C procedure, testing was done to make sure the dialog windows work well with the progress bar etc. Please advise if the computationally intensive C procedure resulted in this issue. If yes, how do I overcome it. searcher08

    C D 2 Replies Last reply
    0
    • S searcher08

      I have completed this dialog window reading in filenames and a progress bar to indicate progress. A timer is used to update the progress bar. However, when I called a C procedure that is computationally intensive, all the edit boxes and menu went blank. The worst thing was that the progress bar was not incrementing. Do take note that the program is running fine except for these side effects. The processed output was tested to be correct. Before I include the C procedure, testing was done to make sure the dialog windows work well with the progress bar etc. Please advise if the computationally intensive C procedure resulted in this issue. If yes, how do I overcome it. searcher08

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Your app needs to be multithreaded, so the UI updates while your code is taking up lots of CPU. Otherwise, the paint messages are not handled, and your window is not drawn.

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

      X S 2 Replies Last reply
      0
      • C Christian Graus

        Your app needs to be multithreaded, so the UI updates while your code is taking up lots of CPU. Otherwise, the paint messages are not handled, and your window is not drawn.

        Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

        X Offline
        X Offline
        XuShanghua
        wrote on last edited by
        #3

        Can I look your code! May be I can make it better! and then sent it to you. ---++Xu Shanghua

        H 1 Reply Last reply
        0
        • X XuShanghua

          Can I look your code! May be I can make it better! and then sent it to you. ---++Xu Shanghua

          H Offline
          H Offline
          Hamid Taebi
          wrote on last edited by
          #4

          You send a thread to Mr Christian Graus :rolleyes: you can answer directly to sender question:)


          WhiteSky


          1 Reply Last reply
          0
          • S searcher08

            I have completed this dialog window reading in filenames and a progress bar to indicate progress. A timer is used to update the progress bar. However, when I called a C procedure that is computationally intensive, all the edit boxes and menu went blank. The worst thing was that the progress bar was not incrementing. Do take note that the program is running fine except for these side effects. The processed output was tested to be correct. Before I include the C procedure, testing was done to make sure the dialog windows work well with the progress bar etc. Please advise if the computationally intensive C procedure resulted in this issue. If yes, how do I overcome it. searcher08

            D Offline
            D Offline
            Don Fletcher
            wrote on last edited by
            #5

            Calling a function like this periodically (but not too often) during your computations should help: // DoMessagePump - Allow windows message processing to occur int DoMessagePump() { BOOL bCompleteFlag=FALSE; MSG msg; BOOL bDoingBackgroundProcessing=TRUE; Sleep(1); while (::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { if (msg.message == WM_QUIT) { bDoingBackgroundProcessing=FALSE; ::PostQuitMessage(0); break; } if (!AfxGetApp()->PreTranslateMessage(&msg)) { ::TranslateMessage(&msg); ::DispatchMessage(&msg); } } AfxGetApp()->OnIdle(0); // updates user interface AfxGetApp()->OnIdle(1); // frees temporary objects if (bDoingBackgroundProcessing == FALSE) return -1; if (bCompleteFlag == TRUE) return -1; return 0; } This code implements basic message passing (the Windows GUI is based on a cooperative multitasking approach, so your code has to "cooperate" LOL). Christian is of course correct - The best way to do this is put your code into a different thread, but that sometimes adds a layer of complexity you don't always need. Beware the calls to AfxGetApp()->OnIdle() - MFC needs this to "breathe" but it can result in lots of overhead in your computation. The more often you call DoMessagePump() the better the GUI response will be, but the longer your computation will take.

            S 1 Reply Last reply
            0
            • C Christian Graus

              Your app needs to be multithreaded, so the UI updates while your code is taking up lots of CPU. Otherwise, the paint messages are not handled, and your window is not drawn.

              Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

              S Offline
              S Offline
              searcher08
              wrote on last edited by
              #6

              I have not done multi-threading before but looking at the reference books and online tutorial, they are all related to classes or objects. I am only calling a C procedure with a parameter in it. The function needs to be in this form. Do I have to convert my C-procedure to this format? Mine is of the format void decode(char *). UINT MyThreadProc( LPVOID pParam ) // taken from MSDN { CMyObject* pObject = (CMyObject*)pParam; if (pObject == NULL || !pObject->IsKindOf(RUNTIME_CLASS(CMyObject))) return 1; // if pObject is not valid // do something with 'pObject' return 0; // thread completed successfully } Next will be to begin the thread: pNewObject = new CMyObject; AfxBeginThread(MyThreadProc, pNewObject); Where do I put my C-procedure in the above code? How do I know that the thread has ended? Thank you. searcher08

              C 1 Reply Last reply
              0
              • S searcher08

                I have not done multi-threading before but looking at the reference books and online tutorial, they are all related to classes or objects. I am only calling a C procedure with a parameter in it. The function needs to be in this form. Do I have to convert my C-procedure to this format? Mine is of the format void decode(char *). UINT MyThreadProc( LPVOID pParam ) // taken from MSDN { CMyObject* pObject = (CMyObject*)pParam; if (pObject == NULL || !pObject->IsKindOf(RUNTIME_CLASS(CMyObject))) return 1; // if pObject is not valid // do something with 'pObject' return 0; // thread completed successfully } Next will be to begin the thread: pNewObject = new CMyObject; AfxBeginThread(MyThreadProc, pNewObject); Where do I put my C-procedure in the above code? How do I know that the thread has ended? Thank you. searcher08

                C Offline
                C Offline
                Christian Graus
                wrote on last edited by
                #7

                Any function that starts with Afx is part of MFC. You can write threaded code without using MFC, and therefore without this format.

                Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                1 Reply Last reply
                0
                • D Don Fletcher

                  Calling a function like this periodically (but not too often) during your computations should help: // DoMessagePump - Allow windows message processing to occur int DoMessagePump() { BOOL bCompleteFlag=FALSE; MSG msg; BOOL bDoingBackgroundProcessing=TRUE; Sleep(1); while (::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { if (msg.message == WM_QUIT) { bDoingBackgroundProcessing=FALSE; ::PostQuitMessage(0); break; } if (!AfxGetApp()->PreTranslateMessage(&msg)) { ::TranslateMessage(&msg); ::DispatchMessage(&msg); } } AfxGetApp()->OnIdle(0); // updates user interface AfxGetApp()->OnIdle(1); // frees temporary objects if (bDoingBackgroundProcessing == FALSE) return -1; if (bCompleteFlag == TRUE) return -1; return 0; } This code implements basic message passing (the Windows GUI is based on a cooperative multitasking approach, so your code has to "cooperate" LOL). Christian is of course correct - The best way to do this is put your code into a different thread, but that sometimes adds a layer of complexity you don't always need. Beware the calls to AfxGetApp()->OnIdle() - MFC needs this to "breathe" but it can result in lots of overhead in your computation. The more often you call DoMessagePump() the better the GUI response will be, but the longer your computation will take.

                  S Offline
                  S Offline
                  searcher08
                  wrote on last edited by
                  #8

                  I would like to use your method instead of creating another thread. However, the computation procedures are in C and not in C++. I am afraid that the above function may not work in the C code as they are all in MFC. I am calling an extern C procedure from a C++ GUI code. Thanks. searcher08

                  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