Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 03-30-2006, 09:22 PM   #1 (permalink)
jaro
Registered User
 
jaro's Avatar
 
Join Date: Feb 2006
Posts: 9
jaro is on a distinguished road
Timed Function in C

Hi folks,

I was wondering (while a program is running) how to make a specific codes/methods to run on a pre-defined time.
Lets say every 2 minutes this codes/methods ,inside the program, would be executed while the said program is still running.

I try searching about this topic through the net , but most of the decent result that I got is about creating a timer.

Any kind help, may it be psuedo code or links, will be much appreciated.

BTW: I'm using MVC6 and my OS is Win2k. the program mentioned here wait for incomming message.The codes/methods that I'm trying to run in a specific time is some sort of "is alive" message (to make sure that the connection between two computer is still on).


regards,
jaro
jaro is offline   Reply With Quote
Old 03-30-2006, 10:22 PM   #2 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
You need some sort of event processing loop that is the main function of the program, and some variable or object that is a timer. On each loop of your program you can check the status of the timer and once it hits the mark it can trigger your message checking function. In the main event loop you can also do the rest of program's work.
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Old 03-31-2006, 01:20 PM   #3 (permalink)
QUantumAnenome
Code Monkey
 
Join Date: Mar 2005
Posts: 55
QUantumAnenome is on a distinguished road
Send a message via Yahoo to QUantumAnenome
In WinMain add this.

Code:
    SetTimer(hWnd, 0 /* id number */, 120000 /* milliseconds */, NULL);
In WndProc add this.

Code:
    switch(iMsg)
    {
    case WM_TIMER:
        if (wParam == 0)
        {
            // your stuff here
        }
        break;
    }
QUantumAnenome is offline   Reply With Quote
Old 04-20-2006, 11:00 PM   #4 (permalink)
jaro
Registered User
 
jaro's Avatar
 
Join Date: Feb 2006
Posts: 9
jaro is on a distinguished road
Hi everyone!
figure out the solution (thanks to some people over the net) . I would like to hear some comment about the solution that I've made.
here's the code: (note this is a test code, to be inserted to a working project)
Code:
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <windows.h>

int x;
char WRITEFILE[50];
char string[200];

int x;
static DWORD	dwDelay = 60000; /* 60000 milliseconds = 60 seconds = 1 minute*/
static DWORD dwTickCount;



int writeToFile(char* str)
{
	FILE* log;

	strcpy(WRITEFILE,"C:\\Dummy.txt");

	log = fopen(WRITEFILE, "a+");
		if (log == NULL)
		{
			return -1;
		}
	fprintf(log, "%s\n",str);
	fclose(log);
	return 0;
}


int main()
{

	
	char timestamp[50];
	time_t t;
	t = time(NULL);

	printf("Start of testProgram.\n");
	writeToFile("Start of testProgram.");
	
	sprintf(timestamp,"%s",ctime(&t));
	timestamp[strlen(timestamp)-1] = '\0';
	writeToFile(timestamp);
	
	dwTickCount = GetTickCount();
	x=0;
	
		while(1)
		{
				if(GetTickCount() - dwTickCount > dwDelay) /* Has 60 seconds elapsed?*/
				{ 
					x++;
					printf("Round %d \n",x);		//print to console 
					sprintf(string,"Round %d",x);	//write to a file
					writeToFile(string);
					dwTickCount = GetTickCount();
				}//end if

		}//end while
	
}//end main
The program will (in every 1 minute) print to console and write to a file a series of increamenting Round.

any comment will be much appreciated.

regards,
jaro
jaro is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
help in C code using client lib function of sybase ashok rajagopal Standard C, C++ 3 11-22-2005 03:04 AM
operate overloading member function in C# sureshkumar_kc MS Technologies ( ASP, VB, C#, .NET ) 2 10-15-2004 02:36 AM
C# member function sureshkumar_kc MS Technologies ( ASP, VB, C#, .NET ) 2 10-15-2004 02:08 AM
edit? anon Lounge 10 11-21-2002 03:02 PM


All times are GMT -8. The time now is 09:39 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting