GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 06-Apr-2006, 01:54
jaro's Avatar
jaro jaro is offline
Junior Member
 
Join Date: Nov 2005
Location: somewhere in souteast asia
Posts: 54
jaro will become famous soon enough

Help. Need some clarification.


Hi,

I have these ff. code:
CPP / C++ / C Code:
/*test/dummy progam*/

#include <time.h>
//var declaration
clock_t start, finish;
double  duration;
double  limit = 5.00; // roughly equal to 5 seconds
int x;

int main(){
    //
    //
    //
    start = clock();
    x=1;

        while(1){

            finish = clock();
            duration = (finish - start) / CLOCKS_PER_SEC;
            if(limit < duration){
                 /* do stuff....*/      
                 printf("round %d",x++); // for example
                start = clock();
                duration =0.00;
            }
        }
return 0;
}

I know that this is not resourece friendly type of code. The code above is a test/dummy program ,the program will wait at least 5 sec. before it will execute the printf().
The test/dummy program works fine but when I try to insert this logic to the project that I'm currently working on , it does not work.
The project mentioned here is converted/or run as a Windows service.

I've written the test/dummy program as a Win32 console application. The project that I've mentioned is previously a Win32 console application before it is converted to run as a windows service.

Can anyone tell me why this is happening?

I'm Using MVC6 and my os is Win2K.

BTW: when I tried to convert the test/dummy program run run as a Windows service, the test/dummy program does not execute - I mean does not print ( I've change the printf() to a writeToLog type of function).

any kind of help will be appreciated.

regards,
jaro
__________________
Action.....reAction
  #2  
Old 06-Apr-2006, 05:51
Brenton S. Brenton S. is offline
New Member
 
Join Date: Mar 2006
Location: Piscataway, NJ
Posts: 21
Brenton S. is on a distinguished road
Smile

Re: Help. Need some clarification.


Hi jari,

How about this:

Code:
#include <windows.h> #include <stdio.h> int main() { int x = 0; while(true) { Sleep(5000); // pause for 5 secs printf("Round: %d", x ++); } return 0; }
  #3  
Old 06-Apr-2006, 06:43
jaro's Avatar
jaro jaro is offline
Junior Member
 
Join Date: Nov 2005
Location: somewhere in souteast asia
Posts: 54
jaro will become famous soon enough

Re: Help. Need some clarification.


Quote:
Originally Posted by Brenton S.
Hi jari,

How about this:

CPP / C++ / C Code:
#include <windows.h>
#include <stdio.h>

int main() {

	int x = 0;

	while(true) {

		Sleep(5000); // pause for 5 secs

		printf("Round: %d", x ++);
	}

	return 0;
}


Hi Brenton,
your suggestion would not do because I need the program to keep on running and would execute a certain code (in this case print "round" in every 5 seconds).
anyway thanks for your suggestion.
Regards,
jaro
__________________
Action.....reAction
  #4  
Old 06-Apr-2006, 06:57
jaro's Avatar
jaro jaro is offline
Junior Member
 
Join Date: Nov 2005
Location: somewhere in souteast asia
Posts: 54
jaro will become famous soon enough

Re: Help. Need some clarification.


Quote:
the program will wait at least 5 sec. before it will execute the printf().

Ops Sorry my mistake. It should have been - while the program is running, in every 5 secs it will execute the printf().

Regards,
jaro
__________________
Action.....reAction
  #5  
Old 06-Apr-2006, 10:15
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,234
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Help. Need some clarification.


You want to run the program as a service. A service is a background function to the OS. Where would it print? There's no screen/window.
__________________

Cow: You're a lawyer too?
Mooseblood (mosquito): Ma'am, I was already a bloodsucking parasite. All I needed was a briefcase!
  #6  
Old 06-Apr-2006, 12:59
Brenton S. Brenton S. is offline
New Member
 
Join Date: Mar 2006
Location: Piscataway, NJ
Posts: 21
Brenton S. is on a distinguished road
Smile

Re: Help. Need some clarification.


Hi jaro,

Is this better:

CPP / C++ / C Code:
#include <windows.h>
#include <stdio.h>

int main() {

	int		x = 1;
	DWORD	dwDelay = 5000; /* 5000 milliseconds = 5 seconds */

	while(true) {

		static DWORD dwTickCount = GetTickCount();

		if(GetTickCount() - dwTickCount > 5000) { /* Has 5 seconds elapsed? */

			printf("Round: %d", x); /* Print message */

			x ++; /* Update 'x' */

			dwTickCount = GetTickCount();
		}
		else {
			/* Do other stuff... */
		}
	}

	return 0;
}
  #7  
Old 07-Apr-2006, 00:30
jaro's Avatar
jaro jaro is offline
Junior Member
 
Join Date: Nov 2005
Location: somewhere in souteast asia
Posts: 54
jaro will become famous soon enough

Re: Help. Need some clarification.


Quote:
Originally Posted by WaltP
You want to run the program as a service. A service is a background function to the OS. Where would it print? There's no screen/window.

When I convert the program to run as a service ,I've change the printf() part to a writeToFile (in this case a text file). I've tested the program (with the writeTofile ) before converting it to run a service.
__________________
Action.....reAction
  #8  
Old 07-Apr-2006, 00:33
jaro's Avatar
jaro jaro is offline
Junior Member
 
Join Date: Nov 2005
Location: somewhere in souteast asia
Posts: 54
jaro will become famous soon enough

Re: Help. Need some clarification.


Quote:
Originally Posted by Brenton S.
Hi jaro,

Is this better:

CPP / C++ / C Code:
#include <windows.h>
#include <stdio.h>

int main() {

	int		x = 1;
	DWORD	dwDelay = 5000; /* 5000 milliseconds = 5 seconds */

	while(true) {

		static DWORD dwTickCount = GetTickCount();

		if(GetTickCount() - dwTickCount > 5000) { /* Has 5 seconds elapsed? */

			printf("Round: %d", x); /* Print message */

			x ++; /* Update 'x' */

			dwTickCount = GetTickCount();
		}
		else {
			/* Do other stuff... */
		}
	}

	return 0;
}


thanks Brenton, I'll try this and will post a feedback soon.

regards,
jaro
__________________
Action.....reAction
  #9  
Old 07-Apr-2006, 04:00
jaro's Avatar
jaro jaro is offline
Junior Member
 
Join Date: Nov 2005
Location: somewhere in souteast asia
Posts: 54
jaro will become famous soon enough

Re: Help. Need some clarification.


Just found out what causing the bug that I mention on my first post.
And guess what it has nothing to do with the code that I've posted,silly me

well anyway thanks to all the people who reply to my thread.

BTW: Brenton,I opt to use your solution (code you've last posted) much better than mine.

regards,
jaro
__________________
Action.....reAction
  #10  
Old 09-Apr-2006, 05:07
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,234
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Help. Need some clarification.


Quote:
Originally Posted by jaro
Just found out what causing the bug that I mention on my first post.
And guess what it has nothing to do with the code that I've posted,silly me

well anyway thanks to all the people who reply to my thread.

BTW: Brenton,I opt to use your solution (code you've last posted) much better than mine.

regards,
jaro
So what was the problem and solution? Maybe your solution might help someone else next time.
__________________

Cow: You're a lawyer too?
Mooseblood (mosquito): Ma'am, I was already a bloodsucking parasite. All I needed was a briefcase!
 

Recent GIDBlogFirst week of IA training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
base case clarification tgtg C Programming Language 1 20-Jul-2005 08:23
dirent, stat, compar & alphasort Old_Spen C Programming Language 5 11-May-2005 08:46
Sorting Array Logic ? karthikeyansen C Programming Language 26 06-Apr-2005 08:01

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 03:21.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.