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. simplest thread program doesn't work

simplest thread program doesn't work

Scheduled Pinned Locked Moved C / C++ / MFC
question
6 Posts 6 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
    Spiritofamerica
    wrote on last edited by
    #1

    DWORD WINAPI tredu (LPVOID lpParam ); int APIENTRY WinMain(HINSTANCE a,HINSTANCE b,LPSTR comanda,int arata) { HANDLE hThread; DWORD dwID,dwThrdParam = 1; hThread = CreateThread (NULL, 0,tredu ,&dwThrdParam, 0, &dwID); if (hThread==NULL) MessageBox(0,"a","a",MB_OK); return 0; } DWORD WINAPI tredu (LPVOID lpParam ) { Beep(1000,1000); MessageBox(0,"a","b",MB_OK); return 0; } this does nothing? where am I going wrong the handle is not NULL but nothing happens I am using VC 7.0

    C H M B L 5 Replies Last reply
    0
    • S Spiritofamerica

      DWORD WINAPI tredu (LPVOID lpParam ); int APIENTRY WinMain(HINSTANCE a,HINSTANCE b,LPSTR comanda,int arata) { HANDLE hThread; DWORD dwID,dwThrdParam = 1; hThread = CreateThread (NULL, 0,tredu ,&dwThrdParam, 0, &dwID); if (hThread==NULL) MessageBox(0,"a","a",MB_OK); return 0; } DWORD WINAPI tredu (LPVOID lpParam ) { Beep(1000,1000); MessageBox(0,"a","b",MB_OK); return 0; } this does nothing? where am I going wrong the handle is not NULL but nothing happens I am using VC 7.0

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

      Your main thread exits as soon as it has created the second thread, so probably it destroys the second thread.

      1 Reply Last reply
      0
      • S Spiritofamerica

        DWORD WINAPI tredu (LPVOID lpParam ); int APIENTRY WinMain(HINSTANCE a,HINSTANCE b,LPSTR comanda,int arata) { HANDLE hThread; DWORD dwID,dwThrdParam = 1; hThread = CreateThread (NULL, 0,tredu ,&dwThrdParam, 0, &dwID); if (hThread==NULL) MessageBox(0,"a","a",MB_OK); return 0; } DWORD WINAPI tredu (LPVOID lpParam ) { Beep(1000,1000); MessageBox(0,"a","b",MB_OK); return 0; } this does nothing? where am I going wrong the handle is not NULL but nothing happens I am using VC 7.0

        H Offline
        H Offline
        Hans Ruck
        wrote on last edited by
        #3

        The main thread ends too soon. Try:

        if (hThread==NULL)
        MessageBox(0,"a","a",MB_OK);
        else
        MessageBox(0,"b","a",MB_OK);


        "tous les sifflets des trains, toutes les sirènes des bateaux   m'ont chanté cent fois la chanson de l'Eldorado" rechi+

        1 Reply Last reply
        0
        • S Spiritofamerica

          DWORD WINAPI tredu (LPVOID lpParam ); int APIENTRY WinMain(HINSTANCE a,HINSTANCE b,LPSTR comanda,int arata) { HANDLE hThread; DWORD dwID,dwThrdParam = 1; hThread = CreateThread (NULL, 0,tredu ,&dwThrdParam, 0, &dwID); if (hThread==NULL) MessageBox(0,"a","a",MB_OK); return 0; } DWORD WINAPI tredu (LPVOID lpParam ) { Beep(1000,1000); MessageBox(0,"a","b",MB_OK); return 0; } this does nothing? where am I going wrong the handle is not NULL but nothing happens I am using VC 7.0

          M Offline
          M Offline
          Marc Soleda
          wrote on last edited by
          #4

          It works fine in VC6. I can't test in VC7 ... ... she said you are the perfect stranger she said baby let's keep it like this... Tunnel of Love, Dire Straits.

          1 Reply Last reply
          0
          • S Spiritofamerica

            DWORD WINAPI tredu (LPVOID lpParam ); int APIENTRY WinMain(HINSTANCE a,HINSTANCE b,LPSTR comanda,int arata) { HANDLE hThread; DWORD dwID,dwThrdParam = 1; hThread = CreateThread (NULL, 0,tredu ,&dwThrdParam, 0, &dwID); if (hThread==NULL) MessageBox(0,"a","a",MB_OK); return 0; } DWORD WINAPI tredu (LPVOID lpParam ) { Beep(1000,1000); MessageBox(0,"a","b",MB_OK); return 0; } this does nothing? where am I going wrong the handle is not NULL but nothing happens I am using VC 7.0

            B Offline
            B Offline
            Bob Ciora
            wrote on last edited by
            #5

            As mentioned, the main is exiting before the thread can even execute. To wait for the thread to terminate, you can do a WaitForSingleObject(hThread, INFINITE);. This will wait for the thread handle to become "invalid," which occurs when the thread terminates. At that point, you should also do a CloseHandle(hThread); before exiting. Bob Ciora

            1 Reply Last reply
            0
            • S Spiritofamerica

              DWORD WINAPI tredu (LPVOID lpParam ); int APIENTRY WinMain(HINSTANCE a,HINSTANCE b,LPSTR comanda,int arata) { HANDLE hThread; DWORD dwID,dwThrdParam = 1; hThread = CreateThread (NULL, 0,tredu ,&dwThrdParam, 0, &dwID); if (hThread==NULL) MessageBox(0,"a","a",MB_OK); return 0; } DWORD WINAPI tredu (LPVOID lpParam ) { Beep(1000,1000); MessageBox(0,"a","b",MB_OK); return 0; } this does nothing? where am I going wrong the handle is not NULL but nothing happens I am using VC 7.0

              L Offline
              L Offline
              LighthouseJ
              wrote on last edited by
              #6

              I agree with cedric. I recommend you look into events, they help control synchronization between the thread and your app. Here's what I'd usually do: struct threadPkg {   HANDLE hStartup, hStopNow, hShutdown; }; UINT ThreadFunc (LPVOID lpParam) {   threadPkg * pkg = (threadPkg *) lpParam;   ::SetEvent(pkg->hStartup);   while (true) {     switch (::WaitForSingleObject(pkg->hStopNow, 10)) {       case WAIT_OBJECT_0: {         ::SetEvent(pkg->hShutdown);         AfxEndThread(0, true);         return 0;           } break;       case WAIT_TIMEOUT: {         // do some work here           } break;       }    } } bool StartThread (threadPkg * pkg) {   CWinThread * thread = AfxBeginThread(ThreadFunc, pkg, THREAD_PRIORITY_IDLE, 0, 0, NULL);   switch (::WaitForSingleObject(pkg->hStartup, INFINITE)) {     case WAIT_OBJECT_0: { return true; } break;     case WAIT_TIMEOUT: {    // should never be reached, INFINITE is a very long time,     return false;        // but I like to totally define all possible program paths         } break;     } } void StopThread (threadPkg * pkg) {   ::SetEvent(pkg->hStopNow);   switch (::WaitForSingleEvent(pkg->hShutdown, INFINITE)) {     case WAIT_OBJECT_0: break;   } } void main () {   threadPkg * pkg = new threadPkg;   pkg->hStartup = ::CreateEvent(NULL, true, false, "startup sequence, thread->main");   pkg->hStopNow = ::CreateEvent(NULL, true, false, "signal to stop now, main->thread");   pkg->hShutdown = ::CreateEvent(NULL, true, false, "thread has shut down, thread->main");   if (StartThread(pkg)) {     // thread is running, do what I need     StopThread(pkg);   } else {     MessageBox(NULL, "Error: The worker thread was not started.", "Critica

              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