SetTimer,TimerProc



  • Hallo,

    ich möchte, dass jede Sekunde am Bildschirm (Konsole) der Text "Hallo" erscheint.
    Was mache ich falsch?

    #include <Windows.h>
    #include <stdio.h>
    
    void CALLBACK MyTimerProc( 
        HWND hwnd,        // handle to window for timer messages 
        UINT message,     // WM_TIMER message 
        UINT idTimer,     // timer identifier 
        DWORD dwTime)     // current system time 
    { 
    	printf("\nHallo.");
    
    } 
    
    int main()
    {
    	UINT_PTR ptr= SetTimer(NULL,NULL,1000,&(TIMERPROC)MyTimerProc);
    
    	return 0;
    }
    

    Danke



  • Dein Programm wird beendet bevor ein callback ausgelöst wird. Versuche ein Sleep(); vor dem return 0; in main().
    Kurt



  • msdn schrieb:

    If your application creates a timer without specifying a window handle, your application must monitor the message queue for WM_TIMER messages and dispatch them to the appropriate window. Note that GetMessage can return -1 if there is an error.

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/timers/usingtimers.asp



  • Hallo,

    soll ich dann wohl Sleep anstatt SetTimer verwenden?



  • ga_un schrieb:

    Hallo,

    soll ich dann wohl Sleep anstatt SetTimer verwenden?

    Nein so war das nicht gemeint. Du sollst zb. Sleep(5000); in der main vor dem return aufrufen um windows die chance zu geben deine Timerproc auch aufzurufen. Denn der callback würde sonst erst nachden dein Programm schon beendet ist passieren. Also nie.

    miller_m schrieb:

    msdn schrieb:

    If your application creates a timer without specifying a window handle, your application must monitor the message queue for WM_TIMER messages and dispatch them to the appropriate window. Note that GetMessage can return -1 if there is an error.

    Er ruft sie aber mit 0 auf und Timerproc ist != 0 also müsste das schon funktionieren.
    Kurt



  • Nachrichtenschleife hast du auch vergessen. (ohne gehts nicht)



  • message loop schrieb:

    Nachrichtenschleife hast du auch vergessen. (ohne gehts nicht)

    Konsolenprogramm 😕



  • na und? trotzdem brauch man sie für die Timer die man mit SetTimer erstellt.

    Dann soll er halt Sleep() benutzen.



  • ZuK schrieb:

    Er ruft sie aber mit 0 auf und Timerproc ist != 0 also müsste das schon funktionieren.
    Kurt

    😕
    da steht nix anderes das wenn du kein fensterhandle mit angibst brauchst du eine
    nachrichtenschleife die WM_TIMER überwacht und evtl. an ein anderes fenster leitet.

    [edit]
    @topic
    benutz doch die forensuche 😉



  • 8.4. Why doesn't SetTimer work in a console app?
    All SetTimer does is tell the system to stick WM_TIMER messages into your window's/thread's message queue after all other messages have been processedand the timer has expired: DefWindowProc secretly invokes the function when it gets this message. Lacking such a queue or a GetMessage()/DispatchMessage() pumping loop the messages never get delivered.

    Also doch ohne Messageloop gehts nicht. Sorry.
    Kurt



  • Hier Junge, so funktionierts!

    void CALLBACK TimerProc(HWND hwnd, UINT message, UINT idTimer, DWORD dwTime);
    
    int main()
    {
    	MSG msg;
    	UINT_PTR ptr;
    
    	ptr = SetTimer(NULL, NULL, 1000, &(TIMERPROC)TimerProc);
    
    	while(GetMessage(&msg, 0, 0, 0))
    	{
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}
    
    	return 0;
    }
    
    void CALLBACK TimerProc(HWND hwnd, UINT message, UINT idTimer, DWORD dwTime)
    { 
    	switch(message)
    	{
    	case WM_TIMER:
    		cout << "hallo";
    		break;
    	}
    }
    

    Ich hoffe es hat dir geholfen.
    Ist ja auch schon ziemlich alt dieser Thread, aber naja. Schadet ja nichts die Lösung zu posten. 😉

    #include <windows.h>
    #include <iostream>

    D4rK3y (SDS)


Anmelden zum Antworten