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. 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.
  • T Offline
    T Offline
    T1TAN
    wrote on last edited by
    #1

    hi everyone, i've started doing my first 'really multithreaded' app. i've inherited the CWinThread, thus creating an interface thread. the problem is, that i sometimes run into some trouble. but first, the code (without project:-)) // this is the thread code BOOL CFilesystemThread::InitInstance() { TRACE("Filesystem thread init...."); m_pFilesystemDlg = new CFilesystemDlg(); m_pFilesystemDlg->Create( IDD_FILESYSTEM_DIALOG, CWnd::GetDesktopWindow() ); m_pFilesystemDlg->ShowWindow( SW_SHOW ); TRACE("..done!\n"); return TRUE; } thread exit: int CFilesystemThread::ExitInstance() { TRACE("Filesystem thread stop...."); if ( m_pFilesystemDlg ) { m_pFilesystemDlg->DestroyWindow(); // i'm unsure about using this, i don't like the warning // "OnDestroy will not be called" etc.. // m_pFilesystemDlg->SendMessage( WM_CLOSE ); // m_pFilesystemDlg->SendMessage( WM_QUIT ); // m_pFilesystemDlg->SendMessage( WM_DESTROY ); delete m_pFilesystemDlg; m_pFilesystemDlg = NULL; } TRACE("..done!\n"); return CWinThread::ExitInstance(); } // and this is how the thread is started, and stopped void CCNWServerDlg::OnBnFilesystem() { if ( g_dataEx.filesystem.pFilesystemInterface.IsLoaded() ) { if ( g_dataEx.pFilesystemThread == NULL ) { g_dataEx.pFilesystemThread = (CFilesystemThread*) AfxBeginThread( RUNTIME_CLASS( CFilesystemThread ) ); } else { g_dataEx.pFilesystemThread->m_pFilesystemDlg->ShowWindow( SW_RESTORE ); g_dataEx.pFilesystemThread->m_pFilesystemDlg->SetForegroundWindow(); } } else { // filesystem dll not found!? MessageBox( "Error loading filesystem module!", "Fatality", MB_OK|MB_ICONSTOP ); } } --- if ( g_dataEx.pFilesystemThread != NULL ) { g_dataEx.pFilesystemThread->ExitInstance(); delete g_dataEx.pFilesystemThread; } the final result is that if i run ONLY the filesystem thread, it goes down gracefuly. if i run for example accounts thread AND the filesystem i get this: debug window: Accounts thread init. Accounts thread stop. Filesystem thread stop......done! Second Chance Assertion Failed: File wincore.cpp, Line 304 message box: User breakpoint called from code at 0x77f7f570. (disassembly opens, which i don't know nothing about..) em...SO. if you have any clue what happens, and where i'm doing things wrong, PLEASE help. anyone..?:sigh: --- kick ash. http://sprdsoft.bigmoron.com http://t1tan.cjb.net

    J 1 Reply Last reply
    0
    • T T1TAN

      hi everyone, i've started doing my first 'really multithreaded' app. i've inherited the CWinThread, thus creating an interface thread. the problem is, that i sometimes run into some trouble. but first, the code (without project:-)) // this is the thread code BOOL CFilesystemThread::InitInstance() { TRACE("Filesystem thread init...."); m_pFilesystemDlg = new CFilesystemDlg(); m_pFilesystemDlg->Create( IDD_FILESYSTEM_DIALOG, CWnd::GetDesktopWindow() ); m_pFilesystemDlg->ShowWindow( SW_SHOW ); TRACE("..done!\n"); return TRUE; } thread exit: int CFilesystemThread::ExitInstance() { TRACE("Filesystem thread stop...."); if ( m_pFilesystemDlg ) { m_pFilesystemDlg->DestroyWindow(); // i'm unsure about using this, i don't like the warning // "OnDestroy will not be called" etc.. // m_pFilesystemDlg->SendMessage( WM_CLOSE ); // m_pFilesystemDlg->SendMessage( WM_QUIT ); // m_pFilesystemDlg->SendMessage( WM_DESTROY ); delete m_pFilesystemDlg; m_pFilesystemDlg = NULL; } TRACE("..done!\n"); return CWinThread::ExitInstance(); } // and this is how the thread is started, and stopped void CCNWServerDlg::OnBnFilesystem() { if ( g_dataEx.filesystem.pFilesystemInterface.IsLoaded() ) { if ( g_dataEx.pFilesystemThread == NULL ) { g_dataEx.pFilesystemThread = (CFilesystemThread*) AfxBeginThread( RUNTIME_CLASS( CFilesystemThread ) ); } else { g_dataEx.pFilesystemThread->m_pFilesystemDlg->ShowWindow( SW_RESTORE ); g_dataEx.pFilesystemThread->m_pFilesystemDlg->SetForegroundWindow(); } } else { // filesystem dll not found!? MessageBox( "Error loading filesystem module!", "Fatality", MB_OK|MB_ICONSTOP ); } } --- if ( g_dataEx.pFilesystemThread != NULL ) { g_dataEx.pFilesystemThread->ExitInstance(); delete g_dataEx.pFilesystemThread; } the final result is that if i run ONLY the filesystem thread, it goes down gracefuly. if i run for example accounts thread AND the filesystem i get this: debug window: Accounts thread init. Accounts thread stop. Filesystem thread stop......done! Second Chance Assertion Failed: File wincore.cpp, Line 304 message box: User breakpoint called from code at 0x77f7f570. (disassembly opens, which i don't know nothing about..) em...SO. if you have any clue what happens, and where i'm doing things wrong, PLEASE help. anyone..?:sigh: --- kick ash. http://sprdsoft.bigmoron.com http://t1tan.cjb.net

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

      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 D 2 Replies 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

        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