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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CWinThread "issues"

CWinThread "issues"

Scheduled Pinned Locked Moved C / C++ / MFC
debugginghelpcsharpc++com
12 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.
  • J John M Drescher

    I have been using multithreading with MFC for over 5 years and in my experience you will run into problems if you do any GUI code in a thread that is not the main window thread. You must never call any MFC gui code across thread boundries and never create any CViews in a thread that is not the main thread. This is because MFC uses thread local data to store a lot of its info about Doc and view. With that said your problem may be auto delete as CWinThread classes will automatically delete them selves on termination. You should disable this by setting m_bAutoDelete to FALSE and free the object yourself. John

    T Offline
    T Offline
    T1TAN
    wrote on last edited by
    #3

    hi john, i (obviously) forgot to mention that the app i'm doing is dialog based. i still can't figure it out: how can one thread affect the other, because my filesystem thread, when run alone, really works fine..? i'll try the m_bAutoDelete thing, is there something i should know about it? is there a standard way everyone uses to stop a thread? thanx --- kick ash. http://sprdsoft.bigmoron.com http://t1tan.cjb.net

    J 1 Reply Last reply
    0
    • T T1TAN

      hi john, i (obviously) forgot to mention that the app i'm doing is dialog based. i still can't figure it out: how can one thread affect the other, because my filesystem thread, when run alone, really works fine..? i'll try the m_bAutoDelete thing, is there something i should know about it? is there a standard way everyone uses to stop a thread? thanx --- kick ash. http://sprdsoft.bigmoron.com http://t1tan.cjb.net

      J Offline
      J Offline
      John M Drescher
      wrote on last edited by
      #4

      T1TAN wrote: i (obviously) forgot to mention that the app i'm doing is dialog based. Oh. I saw that, I'm sorry about the long rant about doc/view but you still have to be careful of GUI calls with dialog based apps. Did you check the source for wincore.cpp, at Line 304? What version of VC are you using? I looked at my version of vc6 and it is not a valid instruction but it is exactly where I thought the problem to be. I mean it is between the CWnd::FromHandle(HWND hWnd) or CWnd::FromHandlePermanent(HWND hWnd). Niether of these functions will work accross threads. [EDIT] Ok line 304 in wincore.cpp is in VC7. Here:

      CWnd* PASCAL CWnd::FromHandle(HWND hWnd)
      {
      CHandleMap* pMap = afxMapHWND(TRUE); //create map if not exist
      ASSERT(pMap != NULL);

      The assert fails because the handle map is null when you are calling this member from a thread that did not create the window. This is fixed by putting the dialog in the main application thread. [/EDIT] John

      T 1 Reply Last reply
      0
      • J John M Drescher

        T1TAN wrote: i (obviously) forgot to mention that the app i'm doing is dialog based. Oh. I saw that, I'm sorry about the long rant about doc/view but you still have to be careful of GUI calls with dialog based apps. Did you check the source for wincore.cpp, at Line 304? What version of VC are you using? I looked at my version of vc6 and it is not a valid instruction but it is exactly where I thought the problem to be. I mean it is between the CWnd::FromHandle(HWND hWnd) or CWnd::FromHandlePermanent(HWND hWnd). Niether of these functions will work accross threads. [EDIT] Ok line 304 in wincore.cpp is in VC7. Here:

        CWnd* PASCAL CWnd::FromHandle(HWND hWnd)
        {
        CHandleMap* pMap = afxMapHWND(TRUE); //create map if not exist
        ASSERT(pMap != NULL);

        The assert fails because the handle map is null when you are calling this member from a thread that did not create the window. This is fixed by putting the dialog in the main application thread. [/EDIT] John

        T Offline
        T Offline
        T1TAN
        wrote on last edited by
        #5

        argh, argv, argc... John M. Drescher wrote: I'm sorry about the long rant about doc/view heh, it's better than saying nothing, is it?:-D i haven't checked it out (and i'm not at my PC right now..) but i sure will. so.. my dialog cannot be inside a thread? now that sucks. i really need to do this app multithreaded, because my app is a file exchange && chat server, and it would be really awkward to leave it single-threaded. if you have any other ideas for making the app more responsive, now is the time and place :) thanx again --- kick ash. http://sprdsoft.bigmoron.com http://t1tan.cjb.net

        J 1 Reply Last reply
        0
        • T T1TAN

          argh, argv, argc... John M. Drescher wrote: I'm sorry about the long rant about doc/view heh, it's better than saying nothing, is it?:-D i haven't checked it out (and i'm not at my PC right now..) but i sure will. so.. my dialog cannot be inside a thread? now that sucks. i really need to do this app multithreaded, because my app is a file exchange && chat server, and it would be really awkward to leave it single-threaded. if you have any other ideas for making the app more responsive, now is the time and place :) thanx again --- kick ash. http://sprdsoft.bigmoron.com http://t1tan.cjb.net

          J Offline
          J Offline
          John M Drescher
          wrote on last edited by
          #6

          T1TAN wrote: so.. my dialog cannot be inside a thread? You can but in a lot of cases you will run into problems like this. You must not have the parent of the dialog be the main window. You must not call any of the dialog's GUI calls from any other thread than the one the dialog was created in. You must not delete the dialog object from a thread that did not create it. John

          T 1 Reply Last reply
          0
          • J John M Drescher

            T1TAN wrote: so.. my dialog cannot be inside a thread? You can but in a lot of cases you will run into problems like this. You must not have the parent of the dialog be the main window. You must not call any of the dialog's GUI calls from any other thread than the one the dialog was created in. You must not delete the dialog object from a thread that did not create it. John

            T Offline
            T Offline
            T1TAN
            wrote on last edited by
            #7

            John M. Drescher wrote: You can but in a lot of cases you will run into problems like this. nothing is ever easy.. X| John M. Drescher wrote: You must not have the parent of the dialog be the main window. i made it so that the desktop window is the parent, without knowing this:cool: John M. Drescher wrote: You must not call any of the dialog's GUI calls from any other thread than the one the dialog was created in. hm.. does that mean that i should send a message to thread, and then handle it so that the owner thread takes care of the dialog? a man's gotta do what man's gotta do..:suss: thanks john, you're a great help dst --- kick ash. http://sprdsoft.bigmoron.com http://t1tan.cjb.net

            J 1 Reply Last reply
            0
            • T T1TAN

              John M. Drescher wrote: You can but in a lot of cases you will run into problems like this. nothing is ever easy.. X| John M. Drescher wrote: You must not have the parent of the dialog be the main window. i made it so that the desktop window is the parent, without knowing this:cool: John M. Drescher wrote: You must not call any of the dialog's GUI calls from any other thread than the one the dialog was created in. hm.. does that mean that i should send a message to thread, and then handle it so that the owner thread takes care of the dialog? a man's gotta do what man's gotta do..:suss: thanks john, you're a great help dst --- kick ash. http://sprdsoft.bigmoron.com http://t1tan.cjb.net

              J Offline
              J Offline
              John M Drescher
              wrote on last edited by
              #8

              T1TAN wrote: does that mean that i should send a message to thread Yes! John

              1 Reply Last reply
              0
              • J John M Drescher

                I have been using multithreading with MFC for over 5 years and in my experience you will run into problems if you do any GUI code in a thread that is not the main window thread. You must never call any MFC gui code across thread boundries and never create any CViews in a thread that is not the main thread. This is because MFC uses thread local data to store a lot of its info about Doc and view. With that said your problem may be auto delete as CWinThread classes will automatically delete them selves on termination. You should disable this by setting m_bAutoDelete to FALSE and free the object yourself. John

                D Offline
                D Offline
                dreamerzz
                wrote on last edited by
                #9

                hi, With what u have mention above, does it means that we cannot include any updates of the GUI codings in the thread function? I have 2 MFC program, can i use just one button to control these 2 MFC? Btw, this 2 MFC is link by a database(Microsoft Access). How can i run this 2 MFC concurrently and continously with just one Start button and will end it once i click on the Exit button. And I tried using thread function with database. But it has some errors on the recordset that i have used. can u advice me on this issue? Thanks lots.. dReaMerzZ

                J 1 Reply Last reply
                0
                • D dreamerzz

                  hi, With what u have mention above, does it means that we cannot include any updates of the GUI codings in the thread function? I have 2 MFC program, can i use just one button to control these 2 MFC? Btw, this 2 MFC is link by a database(Microsoft Access). How can i run this 2 MFC concurrently and continously with just one Start button and will end it once i click on the Exit button. And I tried using thread function with database. But it has some errors on the recordset that i have used. can u advice me on this issue? Thanks lots.. dReaMerzZ

                  J Offline
                  J Offline
                  John M Drescher
                  wrote on last edited by
                  #10

                  I get back on the rest if I have some time. dreamerzz wrote: And I tried using thread function with database. But it has some errors on the recordset that i have used. can u advice me on this issue? This is because DAO is not multithreaded. There are workarounds for this but it is much better to use ADO to connect to the database. I use ado and connect with any of the threads in my program with no problems at all. A good ado article is here: http://www.codeproject.com/database/caaadoclass1.asp John

                  D 1 Reply Last reply
                  0
                  • J John M Drescher

                    I get back on the rest if I have some time. dreamerzz wrote: And I tried using thread function with database. But it has some errors on the recordset that i have used. can u advice me on this issue? This is because DAO is not multithreaded. There are workarounds for this but it is much better to use ADO to connect to the database. I use ado and connect with any of the threads in my program with no problems at all. A good ado article is here: http://www.codeproject.com/database/caaadoclass1.asp John

                    D Offline
                    D Offline
                    dreamerzz
                    wrote on last edited by
                    #11

                    Hi, Thanks for ur help and advice dReaMerzZ

                    J 1 Reply Last reply
                    0
                    • D dreamerzz

                      Hi, Thanks for ur help and advice dReaMerzZ

                      J Offline
                      J Offline
                      John M Drescher
                      wrote on last edited by
                      #12

                      Sorry I did not get the rest of the info for you. I am behind on a very important deadline and I do not have a lot of time. John

                      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