Login Register






Thread Rating:
  • 0 Vote(s) - 0 Average


Timer in C filter_list
Author
Message
Timer in C #1
So.. I'm doing a timer function in C. I controlled the timer status via a global variable named timerStatus. But when I call the timer function it never stops even I set the value of timerStatus to 0. I think thats because of it is a recursive function.
C code is like this:
Code:
#include <stdio.h>
#include <windows.h>

int Timer(int);
int timerStatus = 0;

int main(void){
timerStatus = 1;
Timer(3000);
timerStatus = 0;

return 0;
}

int Timer(int interval){

if(timerStatus == 1){
Sleep(interval);
//Proccess goes here
printf("%s\n","Test");
Timer(interval);
}

return 0;

}

So my question is; How can I change the value of a variable outside from function while that function is on proccess.

Or any fix to this function is O.K Biggrin

Thanks Smile
Fuck You.

Reply

RE: Timer in C #2
Easy.

Code:
#include <stdio.h>
#include <windows.h>

int Timer(int);
int timerStatus = 0;

int main(void){
    timerStatus = 1;
    Timer(3000);
    timerStatus = 0;
    return 0;
}

int Timer(int interval){
    if(timerStatus == 1){
        Sleep(interval);
        //Proccess goes here
        printf("%s\n","Test");
        // Timer(interval) <-- why was this here?
    }
    return 0;
}

I commented out the buggy line. Wink

Nice to see a new C coder in town.
My Bitcoin address: 1AtxVsSSG2Z8JfjNy9KNFDUN6haeKr7LiP
Give me money by visiting www.google.com here: http://coin-ads.com/6Ol83U

If you want a Bitcoin URL shortener/advertiser, please, use this referral: http://coin-ads.com/register.php?refid=noize

Reply

RE: Timer in C #3
(06-16-2013, 05:14 PM)eifezesta Wrote: What about threads ?

C doesn't supports threads directly and it doesn't have any library that comes with it. However there are some available libraries on the web, I know one -> POSIX Threads Library pthread.h. For more info :- http://www.cs.cf.ac.uk/Dave/C/node29.html

(06-16-2013, 01:54 PM)RootTheSystem Wrote: So.. I'm doing a timer function in C. I controlled the timer status via a global variable named timerStatus. But when I call the timer function it never stops even I set the value of timerStatus to 0. I think thats because of it is a recursive function.
C code is like this:
Code:
#include <stdio.h>
#include <windows.h>

int Timer(int);
int timerStatus = 0;

int main(void){
timerStatus = 1;
Timer(3000);
timerStatus = 0;

return 0;
}

int Timer(int interval){

if(timerStatus == 1){
Sleep(interval);
//Proccess goes here
printf("%s\n","Test");
Timer(interval);
}

return 0;

}

So my question is; How can I change the value of a variable outside from function while that function is on proccess.

Or any fix to this function is O.K Biggrin

Thanks Smile


Your object was not clear to me that what you actually want to make or do.
C has a time.h library for timing functions have a look :- http://www.cplusplus.com/reference/ctime/

Also remember it is not useful to use Sleep() in C as it is not thread safe. Also its is not apart of standard C library.
It depends on the compiler. In some compilers it is there is dos.h opr in some in process.h or in windows.h it depends also there are some other functions like kill().

If you want to create stop watch or something then learn to use the POSIX library

Thank you,
Sincerely,
Psycho_Coder
[Image: OilyCostlyEwe.gif]

Reply

RE: Timer in C #4
(06-16-2013, 05:07 PM)noize Wrote: Easy.

Code:
#include <stdio.h>
#include <windows.h>

int Timer(int);
int timerStatus = 0;

int main(void){
    timerStatus = 1;
    Timer(3000);
    timerStatus = 0;
    return 0;
}

int Timer(int interval){
    if(timerStatus == 1){
        Sleep(interval);
        //Proccess goes here
        printf("%s\n","Test");
        // Timer(interval) <-- why was this here?
    }
    return 0;
}

I commented out the buggy line. Wink

Nice to see a new C coder in town.

Thanks Biggrin The function must be recursive so it can do the proccess repeatedly with an interval. Thats because Timer(interval); was there.
Fuck You.

Reply

RE: Timer in C #5
(06-16-2013, 06:09 PM)RootTheSystem Wrote:
(06-16-2013, 05:07 PM)noize Wrote: Easy.

Code:
#include <stdio.h>
#include <windows.h>

int Timer(int);
int timerStatus = 0;

int main(void){
    timerStatus = 1;
    Timer(3000);
    timerStatus = 0;
    return 0;
}

int Timer(int interval){
    if(timerStatus == 1){
        Sleep(interval);
        //Proccess goes here
        printf("%s\n","Test");
        // Timer(interval) <-- why was this here?
    }
    return 0;
}

I commented out the buggy line. Wink

Nice to see a new C coder in town.

Thanks Biggrin The function must be recursive so it can do the proccess repeatedly with an interval. Thats because Timer(interval); was there.

But in this case it would be endlessly looped, and you said the problem was it never stopped. So, what do you really want to do?
My Bitcoin address: 1AtxVsSSG2Z8JfjNy9KNFDUN6haeKr7LiP
Give me money by visiting www.google.com here: http://coin-ads.com/6Ol83U

If you want a Bitcoin URL shortener/advertiser, please, use this referral: http://coin-ads.com/register.php?refid=noize

Reply

RE: Timer in C #6
(06-16-2013, 05:52 PM)Psycho_Coder Wrote:
(06-16-2013, 05:14 PM)eifezesta Wrote: What about threads ?

C doesn't supports threads directly and it doesn't have any library that comes with it. However there are some available libraries on the web, I know one -> POSIX Threads Library pthread.h. For more info :- http://www.cs.cf.ac.uk/Dave/C/node29.html

(06-16-2013, 01:54 PM)RootTheSystem Wrote: So.. I'm doing a timer function in C. I controlled the timer status via a global variable named timerStatus. But when I call the timer function it never stops even I set the value of timerStatus to 0. I think thats because of it is a recursive function.
C code is like this:
Code:
#include <stdio.h>
#include <windows.h>

int Timer(int);
int timerStatus = 0;

int main(void){
timerStatus = 1;
Timer(3000);
timerStatus = 0;

return 0;
}

int Timer(int interval){

if(timerStatus == 1){
Sleep(interval);
//Proccess goes here
printf("%s\n","Test");
Timer(interval);
}

return 0;

}

So my question is; How can I change the value of a variable outside from function while that function is on proccess.

Or any fix to this function is O.K Biggrin

Thanks Smile


Your object was not clear to me that what you actually want to make or do.
C has a time.h library for timing functions have a look :- http://www.cplusplus.com/reference/ctime/

Also remember it is not useful to use Sleep() in C as it is not thread safe. Also its is not apart of standard C library.
It depends on the compiler. In some compilers it is there is dos.h opr in some in process.h or in windows.h it depends also there are some other functions like kill().

If you want to create stop watch or something then learn to use the POSIX library

Thank you,
Sincerely,
Psycho_Coder

Thanks for you answer Smile I know about multi threaded applications but it seems to be hard to use it in C. Isn't there any easier way to do it? Also why the Sleep() function is not safe? Thanks Smile
Fuck You.

Reply

RE: Timer in C #7
(06-16-2013, 06:15 PM)RootTheSystem Wrote: Thanks for you answer Smile I know about multi threaded applications but it seems to be hard to use it in C. Isn't there any easier way to do it? Also why the Sleep() function is not safe? Thanks Smile

Read Here ;- http://www.gnu.org/software/libc/manual/...eping.html
[Image: OilyCostlyEwe.gif]

Reply

RE: Timer in C #8
(06-16-2013, 06:13 PM)noize Wrote:
(06-16-2013, 06:09 PM)RootTheSystem Wrote:
(06-16-2013, 05:07 PM)noize Wrote: Easy.

Code:
#include <stdio.h>
#include <windows.h>

int Timer(int);
int timerStatus = 0;

int main(void){
    timerStatus = 1;
    Timer(3000);
    timerStatus = 0;
    return 0;
}

int Timer(int interval){
    if(timerStatus == 1){
        Sleep(interval);
        //Proccess goes here
        printf("%s\n","Test");
        // Timer(interval) <-- why was this here?
    }
    return 0;
}

I commented out the buggy line. Wink

Nice to see a new C coder in town.

Thanks Biggrin The function must be recursive so it can do the proccess repeatedly with an interval. Thats because Timer(interval); was there.

But in this case it would be endlessly looped, and you said the problem was it never stopped. So, what do you really want to do?

I want to interfere that timerStatus variable while the function is on proccess. Because I can't change its value while its in endless loop.
Fuck You.

Reply

RE: Timer in C #9
(06-16-2013, 06:22 PM)RootTheSystem Wrote:
(06-16-2013, 06:13 PM)noize Wrote:
(06-16-2013, 06:09 PM)RootTheSystem Wrote:
(06-16-2013, 05:07 PM)noize Wrote: Easy.

Code:
#include <stdio.h>
#include <windows.h>

int Timer(int);
int timerStatus = 0;

int main(void){
    timerStatus = 1;
    Timer(3000);
    timerStatus = 0;
    return 0;
}

int Timer(int interval){
    if(timerStatus == 1){
        Sleep(interval);
        //Proccess goes here
        printf("%s\n","Test");
        // Timer(interval) <-- why was this here?
    }
    return 0;
}

I commented out the buggy line. Wink

Nice to see a new C coder in town.

Thanks Biggrin The function must be recursive so it can do the proccess repeatedly with an interval. Thats because Timer(interval); was there.

But in this case it would be endlessly looped, and you said the problem was it never stopped. So, what do you really want to do?

I want to interfere that timerStatus variable while the function is on proccess. Because I can't change its value while its in endless loop.

Then how about:

Code:
#include <stdio.h>
#include <windows.h>

int Timer(int);
int timerStatus = 0;

int main(void){
    timerStatus = 1;
    Timer(3000);
    timerStatus = 0;
    return 0;
}

int Timer(int interval){
    while (timerStatus == 1){
        Sleep(interval);
        //Proccess goes here
        printf("%s\n","Test");
        // Timer(interval) <-- why was this here?
    }
    return 0;
}

?
My Bitcoin address: 1AtxVsSSG2Z8JfjNy9KNFDUN6haeKr7LiP
Give me money by visiting www.google.com here: http://coin-ads.com/6Ol83U

If you want a Bitcoin URL shortener/advertiser, please, use this referral: http://coin-ads.com/register.php?refid=noize

Reply

RE: Timer in C #10
(06-16-2013, 06:35 PM)noize Wrote:
(06-16-2013, 06:22 PM)RootTheSystem Wrote:
(06-16-2013, 06:13 PM)noize Wrote:
(06-16-2013, 06:09 PM)RootTheSystem Wrote:
(06-16-2013, 05:07 PM)noize Wrote: Easy.

Code:
#include <stdio.h>
#include <windows.h>

int Timer(int);
int timerStatus = 0;

int main(void){
    timerStatus = 1;
    Timer(3000);
    timerStatus = 0;
    return 0;
}

int Timer(int interval){
    if(timerStatus == 1){
        Sleep(interval);
        //Proccess goes here
        printf("%s\n","Test");
        // Timer(interval) <-- why was this here?
    }
    return 0;
}

I commented out the buggy line. Wink

Nice to see a new C coder in town.

Thanks Biggrin The function must be recursive so it can do the proccess repeatedly with an interval. Thats because Timer(interval); was there.

But in this case it would be endlessly looped, and you said the problem was it never stopped. So, what do you really want to do?

I want to interfere that timerStatus variable while the function is on proccess. Because I can't change its value while its in endless loop.

Then how about:

Code:
#include <stdio.h>
#include <windows.h>

int Timer(int);
int timerStatus = 0;

int main(void){
    timerStatus = 1;
    Timer(3000);
    timerStatus = 0;
    return 0;
}

int Timer(int interval){
    while (timerStatus == 1){
        Sleep(interval);
        //Proccess goes here
        printf("%s\n","Test");
        // Timer(interval) <-- why was this here?
    }
    return 0;
}

?

It has to be to do the proccess repeatedly. And can be stopped from outside the function.
Fuck You.

Reply







Users browsing this thread: 1 Guest(s)