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. Thread problem!

Thread problem!

Scheduled Pinned Locked Moved C / C++ / MFC
help
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.
  • D Offline
    D Offline
    Dennis L
    wrote on last edited by
    #1

    Hi all! I have a thread problem in the following code: (This is not the complete code..) Somewhere in a function i have the following implementation: /*****************************************/ // Start of thread HANDLE hThread = CreateThread(...); // Wait until thread ends WaitForSingleObject(hThread, INFINITE); ... /*****************************************/ // Thread procedure DWORD WINAPI ThreadProc(LPVOID lpParam) { // My code ...... ...... SendMessage() <--- The system right here is deadlock (***) return 0; } (***) The SendMessage() function sends message (LVM_INSERTITEM) to a list view control of the window that stared the thread. The problem is that I want the SendMessage() function to work, without removing the WaitForSingleObject() function, or any other "wait" function it might need to be replaced. Thanks for any reply!

    C B 2 Replies Last reply
    0
    • D Dennis L

      Hi all! I have a thread problem in the following code: (This is not the complete code..) Somewhere in a function i have the following implementation: /*****************************************/ // Start of thread HANDLE hThread = CreateThread(...); // Wait until thread ends WaitForSingleObject(hThread, INFINITE); ... /*****************************************/ // Thread procedure DWORD WINAPI ThreadProc(LPVOID lpParam) { // My code ...... ...... SendMessage() <--- The system right here is deadlock (***) return 0; } (***) The SendMessage() function sends message (LVM_INSERTITEM) to a list view control of the window that stared the thread. The problem is that I want the SendMessage() function to work, without removing the WaitForSingleObject() function, or any other "wait" function it might need to be replaced. Thanks for any reply!

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      This will never work !! I suppose the function which creates the Thread is not in another thread than your application ? Thus your application will freeze because of the WaitForSingleObject call. And because of that, it cannot process messages anymore (so it will not respond to user events neither). It is not a good idea to call WaitForSingleObject in the main thread. A better idea would be that, when your secondary thread finishes, it sends a message (an owner defined message) to the main window resulting in calling a specific function that will manage every that needs to be done when your thread finishes.

      1 Reply Last reply
      0
      • D Dennis L

        Hi all! I have a thread problem in the following code: (This is not the complete code..) Somewhere in a function i have the following implementation: /*****************************************/ // Start of thread HANDLE hThread = CreateThread(...); // Wait until thread ends WaitForSingleObject(hThread, INFINITE); ... /*****************************************/ // Thread procedure DWORD WINAPI ThreadProc(LPVOID lpParam) { // My code ...... ...... SendMessage() <--- The system right here is deadlock (***) return 0; } (***) The SendMessage() function sends message (LVM_INSERTITEM) to a list view control of the window that stared the thread. The problem is that I want the SendMessage() function to work, without removing the WaitForSingleObject() function, or any other "wait" function it might need to be replaced. Thanks for any reply!

        B Offline
        B Offline
        Bob Stanneveld
        wrote on last edited by
        #3

        Hello, What you are doing is asking for trouble! The rule of thumb is: never touch the GUI from your worker thread!!!! If you really need to do this, try the following: use PostMessage() instead of SendMessage(). If you really need to wait for the thread to finish, use MsgWaitForMultipleObjects() instead of WaitForSingleObject(). This way, your app won't freeze because it will process messages. Hope this helps. Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

        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