Timer error

#include<windows.h>
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

SYSTEMTIME st,lt;
int timerproc(void);

int main()
{
int crrent_time;


GetSystemTime(&st);
GetLocalTime(&lt);
printf("The system time is: %02d:%02d\n", st.wHour, st.wMinute);
printf(" The local time is: %02d:%02d\n", lt.wHour, lt.wMinute);

printf("\nSeting timer\n");
SetTimer(NULL,0,2000,(TIMERPROC)timerproc);
printf("\ntimer set for 2 min..\n");


getch();
return 0;
}

int timerproc(void)
{
GetSystemTime(&st);
printf("The system time is: %02d:%02d\n", st.wHour, st.wMinute);
return 0;
}


I used the above code the code never returns to timerproc what am i doing?
1. Code tags, please. See http://a.ly/5QH .
2. The getch() function blocks the thread. The timer will not spawn a new thread just to run the callback routine. The callback routine is called by the default window procedure when it processes WM_TIMER. See http://msdn.microsoft.com/en-us/library/windows/desktop/ms644906(v=vs.85).aspx for details.
3. That is not a proper TimerProc signature. See http://msdn.microsoft.com/en-us/library/windows/desktop/ms644907(v=vs.85).aspx . Best rule of thumb: If you are forced to type cast to be able to compile, you are probably doing something wrong. Your problem here is your complete disregard for the documentation. You used a function signature that you felt like using instead of the correct one.
sir can u just correct the above code?
What WM_TIMER message when this is a console application ?

This is not windows 95 and Microsoft does not recommend using SetTimer() in a console app.
SetTimer() was not designed to be used with a console application because it requires a message loop to dispatch the timer signal to the timer procedure. In a console application, this behavior can be easily emulated with a thread that is set to wait on an event.
*Snaps a whip at webJose* You heard him! Correct his code, knave! What do you think we're not paying you for? :|

Serously though, Tarun, if you can't even be bothered putting your code in code tags why do you think we should be bothered to fix your errors?
^ lol
Sorry sir i will put my code in tags from now .... can u tell me now that how can i use a timer in console application....
You can't. Those timer functions are only available to Windows applications.
is there any any way by which i can use timer function in console application or any other timer function available for console applicaticon.
If you explain why you need a timer, we might be able to suggest something suitable.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80


#include <stdio.h>
#include <time.h>
#include <conio.h>
#include<windows.h>

time_t now;
struct tm* tm;
time_t now=time(NULL);
struct tm *tm=localtime(&now);
int o2,o1;
time_t s2,s1;


void play() {
  // Open the input file as a 'byte-stream file source':

  fi_params.nFICardFrameSize = TRANSPORT_PACKETS_PER_NETWORK_PACKET * TRANSPORT_PACKET_SIZE;
  fi_params.p_lm_lock_fn = lm_lock_fn;
  fi_params.p_lm_unlock_fn = lm_unlock_fn;

  DeviceParameters temp;

	fileSource = DeviceSourceFICard::createNew(*env, fi_params, temp);
  if (fileSource == NULL) {
    *env << "Unable to open file \"" << inputFileName
         << "\" as a byte-stream file source\n";
    exit(1);
  }
  FramedSource* videoES = fileSource;
  
  // Create a framer for the Video Elementary Stream:
  videoSource = MPEG1or2VideoStreamDiscreteFramer::createNew(*env, videoES);
 
  // Finally, start playing:
  *env << "Beginning to read from file...\n";

	o1=videoSink->octetCount();
	s1=tm->tm_sec;


	videoSink->startPlaying(*videoSource, afterPlaying, videoSink);

	int Counter=0;
	MSG Msg;
	UINT TimerId = SetTimer(NULL, 0, 10000, &TimerProc);
	cout << "TimerId: " << TimerId << '\n';
	//printf("TimerId %s/n",TimerId);
	if (!TimerId)
		return 16;
	while (GetMessage(&Msg, NULL, 0, 0)) {
		++Counter;
		if (Msg.message == WM_TIMER)
			cout << "Counter: " << Counter << "; timer message\n";
		/*printf("Counter: %d\n",Counter);*/
		else
			cout << "Counter: " << Counter << "; message: " << Msg.message << '\n';
		/*printf("Counter: %d\n",Counter);
		printf(" message: %s\n",Msg.message);*/
		DispatchMessage(&Msg);
	}
	KillTimer(NULL, TimerId);
 
}



VOID CALLBACK TimerProc(HWND hWnd, UINT nMsg, UINT nIDEvent, DWORD dwTime) {
cout << "Time: " << dwTime << '\n';
//printf("Time: %s/n",dwTime);
//getch();
cout.flush();
o2=videoSink->octetCount();
	s2=tm->tm_sec;
	double mbits_sent = (o2 - o1) / 1024.0 / 1024.0 / (s2 - s1);
	printf("mbits_sent %d\n",mbits_sent);

}



i need to calculate the bit rate as shown above as bit rate is bits per second so need a timer to check data after 1 second .i.e. need to call videoSink->octetCount(); method after 1 second.its an console application
closed account (DSLq5Di1)
You can create a message loop in a console application and use SetTimer().. but it's probably not the best idea, have a look at using Timer Queues:- http://msdn.microsoft.com/en-us/library/windows/desktop/ms687003
Does this start the stream playing asynchronously?
 
videoSink->startPlaying(*videoSource, afterPlaying, videoSink);


If that's the case, you just sleep for 1 sec intervals in a loop.
Topic archived. No new replies allowed.