Hi

I wonder if any kind soul could help me with a timer problem in c.

I am not at all experienced in writing c code.

In a c dll (called from a vb user control) I need to avoid blocking the parent application, and also be able to abort the dll function if the parent terminates. I want to use a timer for this purpose.

VOID CALLBACK TimerProc(HWND, UINT, UINT_PTR, DWORD);

//dllFunction
int iTimerID;

	iTimerID = SetTimer (NULL, 0, 200,  (TIMERPROC)TimerProc);
	
	//do some stuff (may take several seconds)
	
	KillTimer(0,iTimerID);
  return;
  
  
  VOID CALLBACK TimerProc(      
      HWND hwnd,
      UINT uMsg,
      UINT_PTR idEvent,
      DWORD dwTime
  )
  {
     MSG Msg = { 0 };
     while( PeekMessage( &Msg, NULL, 0, 0, PM_REMOVE ) == TRUE)
     {
  
         TranslateMessage( &Msg );
         DispatchMessage( &Msg );
      }
  }

The timer is created and a handle returned. However I am obviously doing something wrong as the callback procedure never gets called.

that doesn't work if your program does not have a message pump. I've never tried it in a dll but I do know it doesn't work in a standard C/C++ language console program.

You might want to write a small C windows program that tests your dll to see if you can get the dll to work like you want it.

Hi Ancient Dragon

Thanks for taking the time to help and apologies for this slow acknowledgement.

I have tried your suggestion and as you say this code does not work. I have found a way to execute a callback to the vb6 caller at reasonably controllable intervals and and can free it up (and if necessary abort the dll function) that way.

Thanks again for your help.

You must process the message OUT from the Callback function.
See the code below. You can run it in windows.
Hope this helps.
oskar

#include "stdafx.h"
#include "windows.h"

VOID CALLBACK Callback1(
  HWND hwnd,         // handle to window
  UINT uMsg,         // WM_TIMER message
  UINT_PTR idEvent,  // timer identifier
  DWORD dwTime       // current system time
)
{
    printf ("Callback1 called\n");
}



// This function MUST be called to get the timers running if you dont have
// a WindowsProc that handles this timer, that is, if you passed NULL in the 
// first parameter of SetTimer
void ProcessTimerMessages()
{
    HWND hwndTimer=NULL;    // handle to window for timer messages 
    MSG msg;                // message structure 

    while (GetMessage(&msg, // message structure 
            NULL,           // handle to window to receive the message 
            NULL,           // lowest message to examine 
            NULL)          // highest message to examine 
           != 0 && GetMessage(&msg, NULL, NULL, NULL) != -1)
    { 

        // Post WM_TIMER messages to the hwndTimer procedure. 

        if (msg.message == WM_TIMER) 
        { 
            msg.hwnd = hwndTimer; 
        } 

        TranslateMessage(&msg); // translates virtual-key codes 
        DispatchMessage(&msg);  // dispatches message to window 
    } 
}

int main(int argc, char* argv[])
{
    UINT_PTR a;

    a = SetTimer(NULL,             
        0,            
        1000,                  // 1-second interval 
        (TIMERPROC) Callback1);  

    ProcessTimerMessages();

    return 0;
}

Sorry. There was a bug in the code of my previous post.
Here it goes correctly.

#include "stdio.h"
#include "windows.h"

VOID CALLBACK Callback1(
  HWND hwnd,         // handle to window
  UINT uMsg,         // WM_TIMER message
  UINT_PTR idEvent,  // timer identifier
  DWORD dwTime       // current system time
)
{
	printf ("Callback1 called\n");
}



// This function MUST be called to get the timers running if you dont have
// a WindowsProc that handles this timer, that is, if you passed NULL in the 
// first parameter of SetTimer
void ProcessTimerMessages()
{
	MSG msg;			
	BOOL ret;
    while (ret=GetMessage(&msg, // message structure 
            NULL,           // handle to window to receive the message 
            NULL,           // lowest message to examine 
            NULL))          // highest message to examine 
	{
		if (ret != -1)
			DispatchMessage(&msg);  // dispatches message to window 
	}
}

int main(int argc, char* argv[])
{
	UINT_PTR a;

	a = SetTimer(NULL,             
		0,            
		250,
		(TIMERPROC) Callback1);  

	ProcessTimerMessages();

	return 0;
}

same program how to write ATL project in Same Timer

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.