Thread läuft nicht ...



  • a) Warum läuft das nicht? Folgender fehler beim compilieren (Borland5.5):

    Error E2034 proxy01.cpp 106: Cannot convert 'unsigned long (*)(void *)' to 'unsigned long (__stdcall *)(void *)' in function __stdcall WndProc(HWND__ *,unsigned int,unsigned int,long)
    Error E2342 proxy01.cpp 106: Type mismatch in parameter 'lpStartAddress' (wanted 'unsigned long (__stdcall *)(void )', got 'unsigned long ()(void *)') in function __stdcall WndProc(HWND__ *,unsigned int,unsigned int,long)

    #define STRICT
    #define IDBUTTONSTART 1
    #define IDBUTTONSTOP 2
    
    #include <windows.h>
    #include <string>
    #include <iostream>
    #include <process.h> 
    
    const char szAppName[] = "Test";
    
    int flag = 1;
    
    DWORD ThreadProc (PVOID par) 
    { 
      while(flag) 
      { 
        Sleep(1000); 
        MessageBeep(0); 
      } 
    } 
    
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
    {
       MSG        msg;
       HWND       hWnd;
       WNDCLASS   wc;
    
       wc.style               = CS_HREDRAW | CS_VREDRAW;
       wc.lpfnWndProc         = WndProc;
       wc.cbClsExtra          = 0;
       wc.cbWndExtra          = 0;
       wc.hInstance           = hInstance;
       wc.hCursor             = LoadCursor(NULL, IDC_ARROW);
       wc.hIcon               = LoadIcon(NULL, IDI_APPLICATION);
       wc.hbrBackground       = (HBRUSH) (COLOR_BTNFACE + 1);
       wc.lpszClassName       = szAppName;
       wc.lpszMenuName        = NULL;
    
       RegisterClass(&wc);
    
       hWnd = CreateWindow(   szAppName,
                              szAppName,
                              WS_OVERLAPPEDWINDOW,
                              CW_USEDEFAULT,
                              CW_USEDEFAULT,
                              240,
                              70,
                              NULL,
                              NULL,
                              hInstance,
                              NULL);
    
        ShowWindow(hWnd, iCmdShow);
        UpdateWindow(hWnd);
    
        while (GetMessage(&msg, NULL, 0, 0))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        return msg.wParam;
    }
    
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        static HWND hButtonStart, hButtonStop;
    
        switch (message)
    	{
            case WM_CREATE:
            {
                hButtonStart = CreateWindow( "button",
                                        "StartTh",
                                        WS_CHILD | WS_VISIBLE,
                                        10, 15, 100, 20,
                                        hWnd,
                                        (HMENU) IDBUTTONSTART,
                                        ((LPCREATESTRUCT) lParam) -> hInstance,
                                        NULL);                 
                SendMessage(hButtonStart, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), 0);
                hButtonStop = CreateWindow( "button",
                                        "StopTh",
                                        WS_CHILD | WS_VISIBLE,
                                        120, 15, 100, 20,
                                        hWnd,
                                        (HMENU) IDBUTTONSTOP,
                                        ((LPCREATESTRUCT) lParam) -> hInstance,
                                        NULL);                 
                SendMessage(hButtonStop, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), 0); 
            }  
            case WM_COMMAND:
            {
                switch(HIWORD(wParam))
                {
                    case BN_CLICKED:
                    {
                        switch(LOWORD(wParam))
                        {                     
                            case IDBUTTONSTART:
                            {
                                flag = 1;
                                HANDLE threadHandle;
                                unsigned long threadId;
                                threadHandle = CreateThread(NULL, 0, ThreadProc, NULL, 0, &threadId);
                                //_beginthread(ThreadProc, 0, &Param);
                                break;
                            }          
                            case IDBUTTONSTOP:
                            {
                                flag = 0;
                                break;
                            } 
                        }
                        return 0;
                    }
                }
                return 0;
            }
            case WM_DESTROY:
    		{
    			PostQuitMessage(0);
    			return 0;
    		}
    	}
    	return DefWindowProc(hWnd, message, wParam, lParam);
    }
    

    b) Mit dem Auskommentierten geht's auch nicht, wo kann / muss ich beim Borland multithread-fähige code generierung einstellen?

    THX 😕



  • Die Fehlermeldung solltest du auch lesen, nicht nur posten. 🙂

    Der Compiler erwartet einen anderen Funktionszeiger als du übergibst; in der Fehlermeldung steht, dass er __stdcall vermisst. Probier mal "DWORD WINAPI ThreadProc(PVOID)".

    btw: _beginthread (bzw. _beginthreadex) ist schon die richtige Variante, von CreateThread würde ich eher Abstand nehmen.



  • Jo thx erstmal. Ich raffe halt manchmal noch nicht ganz, was ich ändern muss ... Zu b) wenn du meinst dass ist schöner, kannst du mir dann auch das Umstellen auf Multithreading (wenn das der fehler ist, wie ich vermute) beim Borland 5.5 erklären?

    Error E2268 proxy01.cpp 107: Call to undefined function '_beginthread' in function __stdcall WndProc(HWND__ *,unsigned int,unsigned int,long)

    Den habe ich gelesen, habe aber ja das <process.h> included ... Habe gelesen, dass man bei anderen Compilern die Codegeneration umstellen muss. In der Hilfe habe ich aber nichts gefunden ... (oder übersehen mag auch sein)



  • cd9000 schrieb:

    btw: _beginthread (bzw. _beginthreadex) ist schon die richtige Variante, von CreateThread würde ich eher Abstand nehmen.

    naja, manchmal kann CreateThread() schon rumzicken:

    msdn schrieb:

    A thread in an executable that is linked to the static C run-time library (CRT) should use _beginthread and _endthread for thread management rather than CreateThread and ExitThread. Failure to do so results in small memory leaks when the thread calls ExitThread. Another work around is to link the executable to the CRT in a DLL instead of the static CRT. Note that this memory leak only occurs from a DLL if the DLL is linked to the static CRT and a thread calls the DisableThreadLibraryCalls function. Otherwise, it is safe to call CreateThread and ExitThread from a thread in a DLL that links to the static CRT.



  • guck dir einfach die compiler flags an. mit bcc32 /? oder so. da muss man noch was für multithreading umstellen...



  • Ich finde da aber nichts 😞 Habe ich oben auch schon geschrieben ... Vielleicht bin ich zu blind oder blöd ...



  • bcc32 -WM test.cpp


Anmelden zum Antworten