simplest thread program doesn't work
-
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 -
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.0Your main thread exits as soon as it has created the second thread, so probably it destroys the second thread.
-
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 -
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.0It 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.
-
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.0As 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 aCloseHandle(hThread);
before exiting. Bob Ciora -
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.0I 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