CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Aug 2019
    Posts
    3

    [RESOLVED] MFC Dialog app - thread doesn't run unless caller is stuck wiating for MessageBox...

    I am developing an MFC dialog application that interfaces to a device through a provided API. One of the functions within the API enters a polling loop, so to keep my dialog active while the API is polling, I have created a thread to handle all of that, but the thread only runs if the calling dialog is stuck waiting for a MessageBox. This is my first MFC app, so forgive my ignorance with much of this.

    I'll try to summarize what I'm doing now:

    The main dialog app is contained in this class:
    class CTelloSPI6Dlg : public CDialogEx

    A public member function in CTelloSPI6Dlg serves as the thread:
    static UINT SPI_Comm_Thread(LPVOID pParam);

    It's started here:
    AfxBeginThread( SPI_Comm_Thread, (LPVOID) &tmpHWND, THREAD_PRIORITY_NORMAL);

    This works, except for one thing: The new thread, SPI_Comm_Thread only functions if the calling dialog app is sitting, waiting for the user to click OK on a MessageBox. In other words, when AfxBeginThread starts the thread, nothing will happen with the thread unless a MessageBox is brought up in the calling code. Once the thread is running, if the MessageBox is closed, then SPI_Comm_Thread stops running. It doesn't exit or terminate, it just quits running.

    Is this because the dialog app is created as a modal dialog?
    Take a look here:
    BOOL CTelloSPI6App::InitInstance()
    {
    ...
    CTelloSPI6Dlg dlg;
    m_pMainWnd = &dlg;
    INT_PTR nResponse = dlg.DoModal();

    Is it possible to change it to a modeless dialog without recreating everything?

    I have tried changing the priority of SPI_Comm_Thread, but that doesn't change anything.

    Thanks!!!

  2. #2
    Join Date
    Nov 2018
    Posts
    120

    Re: MFC Dialog app - thread doesn't run unless caller is stuck wiating for MessageBox


  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: MFC Dialog app - thread doesn't run unless caller is stuck wiating for MessageBox

    Quote Originally Posted by salem_c View Post
    Does this mean we shouldn't answer the question?

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: [RESOLVED] MFC Dialog app - thread doesn't run unless caller is stuck wiating for

    Quote Originally Posted by UpwardBound View Post
    I am developing an MFC dialog application that interfaces to a device through a provided API. One of the functions within the API enters a polling loop, so to keep my dialog active while the API is polling, I have created a thread to handle all of that, but the thread only runs if the calling dialog is stuck waiting for a MessageBox. This is my first MFC app, so forgive my ignorance with much of this.

    I'll try to summarize what I'm doing now:

    The main dialog app is contained in this class:
    class CTelloSPI6Dlg : public CDialogEx

    A public member function in CTelloSPI6Dlg serves as the thread:
    static UINT SPI_Comm_Thread(LPVOID pParam);

    It's started here:
    AfxBeginThread( SPI_Comm_Thread, (LPVOID) &tmpHWND, THREAD_PRIORITY_NORMAL);

    This works, except for one thing: The new thread, SPI_Comm_Thread only functions if the calling dialog app is sitting, waiting for the user to click OK on a MessageBox. In other words, when AfxBeginThread starts the thread, nothing will happen with the thread unless a MessageBox is brought up in the calling code. Once the thread is running, if the MessageBox is closed, then SPI_Comm_Thread stops running. It doesn't exit or terminate, it just quits running.

    Is this because the dialog app is created as a modal dialog?
    Take a look here:
    BOOL CTelloSPI6App::InitInstance()
    {
    ...
    CTelloSPI6Dlg dlg;
    m_pMainWnd = &dlg;
    INT_PTR nResponse = dlg.DoModal();

    Is it possible to change it to a modeless dialog without recreating everything?

    I have tried changing the priority of SPI_Comm_Thread, but that doesn't change anything.

    Thanks!!!
    Not sure why the secondary thread should be independent, but depends on tue implementation. Are you waiting on the worker thread in the main UI thread? If you are, that will lock up the UI.

    At any rate, within the polling loop try adding a PumpMessages call as described in this thread http://forums.codeguru.com/showthrea...n-a-dialog-box. You'll need to pump messages on the hwnd of the main thread.

    As a disclaimer, this isn't the right way to take care of UI/threading issues, but it might get you unblocked.

  5. #5
    Join Date
    Nov 2018
    Posts
    120

    Re: MFC Dialog app - thread doesn't run unless caller is stuck wiating for MessageBox

    Quote Originally Posted by Arjay View Post
    Does this mean we shouldn't answer the question?
    It means everyone here can make an informed choice as to whether it's worth spending time answering the question.

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: MFC Dialog app - thread doesn't run unless caller is stuck wiating for MessageBox

    Quote Originally Posted by salem_c View Post
    It means everyone here can make an informed choice as to whether it's worth spending time answering the question.
    Informed decision? That implies that folks are only allowed to post a question to a single forum. Who made that rule? At any rate, why bother posting if only to say the OP posted elsewhere? The internet would be a nicer place with fewer hall monitors.

  7. #7
    Join Date
    Nov 2018
    Posts
    120

    Re: [RESOLVED] MFC Dialog app - thread doesn't run unless caller is stuck wiating for

    > That implies that folks are only allowed to post a question to a single forum
    http://www.catb.org/esr/faqs/smart-questions.html#forum
    and it's little sister
    http://www.catb.org/esr/faqs/smart-q...ns.html#urgent

    Sure, they're free to post on multiple forums.
    Indeed, if one forum doesn't pan out, they free to go find another one.

    But rampant parallel spam needs to be called out.


    Consider this thought experiment.
    10 forums, 10 newbs, 10 helpers.
    Each newb posts on exactly one forum, everyone gets an answer and no time is wasted.

    vs
    10 forums, 10 newbs, 10 helpers.
    Each newb posts on several forums. Now each helper is overloaded and some questions go unanswered.

    vs
    10 forums, 10 newbs, 10 helpers.
    Each newb posts on all the forums, because the answer rate continues to fall. The only strategy for newbs is to broadcast far and wide in the hopes of getting a bite.

  8. #8
    Join Date
    Aug 2019
    Posts
    3

    Re: MFC Dialog app - thread doesn't run unless caller is stuck wiating for MessageBox

    Quote Originally Posted by Arjay View Post
    Does this mean we shouldn't answer the question?
    Arjay,
    The problem was that I had used a local variable to pass the calling class handle to the thread. A pretty stupid mistake on my part. If I brought up a MessageBox after the thread was started, but before the calling function exited, then the thread would run just fine since the handle would be valid. But once the calling function would exit (if I closed the MessageBox), then the thread would stop functioning. Curiously, it didn't crash the program. Salem, on the cplusplus forum picked up on that given that I had named the handle "tmpHWND."

  9. #9
    Join Date
    Aug 2019
    Posts
    3

    Re: [RESOLVED] MFC Dialog app - thread doesn't run unless caller is stuck wiating for

    So, I guess I need to apologize for posting exactly the same issue on two different sites. I needed this issue fixed pretty quick and needed to pull out the stops to get it done. I was pretty surprised that this was noticed. I just know that often a posting on a forum can take days for a response, so I doubled the exposure to the issue by doing so. But now I know of two excellent forums that are well-travelled. Thank you for your patience. I'll post on only one next time.

  10. #10
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: [RESOLVED] MFC Dialog app - thread doesn't run unless caller is stuck wiating for

    I guess I need to apologize for posting exactly the same issue on two different sites.
    Personally I see nothing wrong with posting the same issue on multiple sites. The only issue I have is that if this is done, then link(s) should be provided to the other site(s) so that viewers and those providing a response can see what already has been suggested etc.

    What, IMO, is really annoying is to take some time to provide an answer - only to find later that the issue had already been resolved elsewhere - or that suggestions were the same as others elsewhere had made, or that further info requested on one site wasn't know to others etc. If the OP provides links, then all of this is avoided.

    I was pretty surprised that this was noticed
    Not really. Many users regularly browse several of these sites - even if they don't actively contribute.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  11. #11
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: [RESOLVED] MFC Dialog app - thread doesn't run unless caller is stuck wiating for

    Quote Originally Posted by salem_c View Post
    > That implies that folks are only allowed to post a question to a single forum
    http://www.catb.org/esr/faqs/smart-questions.html#forum
    and it's little sister
    http://www.catb.org/esr/faqs/smart-q...ns.html#urgent

    Sure, they're free to post on multiple forums.
    Indeed, if one forum doesn't pan out, they free to go find another one.

    But rampant parallel spam needs to be called out.


    Consider this thought experiment.
    10 forums, 10 newbs, 10 helpers.
    Each newb posts on exactly one forum, everyone gets an answer and no time is wasted.

    vs
    10 forums, 10 newbs, 10 helpers.
    Each newb posts on several forums. Now each helper is overloaded and some questions go unanswered.

    vs
    10 forums, 10 newbs, 10 helpers.
    Each newb posts on all the forums, because the answer rate continues to fall. The only strategy for newbs is to broadcast far and wide in the hopes of getting a bite.
    As a moderator and long-time contributor here, I am interested in helping folks get answers to questions they'e posted to CG forums.

    I am less interested in whether a poster has posted elsewhere. For one thing my time is valuable and I don't spend much time on other sites let alone have the time scouring other sites for duplicate posts. The whole point of having different forums is for folks to be able to post in different forums. Having a few duplicate posts out there is insignificant compared to the whole of internet traffic. In other words, we aren't getting charged for duplicates so why worry?

    I am also less interested in how other sites operate. Many sites operate on points awarded for resolved issues and thumbs up or down on post replies. In my opinion this has led to some petty replies and the focus seems to be all about points.

    To me, as a contributor, the bottom line in this forum is about helping to answer questions.

  12. #12
    Join Date
    Oct 2019
    Posts
    8

    Re: [RESOLVED] MFC Dialog app - thread doesn't run unless caller is stuck wiating for

    I don't see anнерштп wrong with posting the same issues on multiple sites.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured