Sinisterly
Timer in C - Printable Version

+- Sinisterly (https://sinister.ly)
+-- Forum: Coding (https://sinister.ly/Forum-Coding)
+--- Forum: C, C++, & Obj-C (https://sinister.ly/Forum-C-C-Obj-C)
+--- Thread: Timer in C (/Thread-Timer-in-C)

Pages: 1 2


Timer in C - Boomslang - 06-16-2013

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


RE: Timer in C - noize - 06-16-2013

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.


RE: Timer in C - Psycho_Coder - 06-16-2013

(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


RE: Timer in C - Boomslang - 06-16-2013

(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.


RE: Timer in C - noize - 06-16-2013

(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?


RE: Timer in C - Boomslang - 06-16-2013

(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


RE: Timer in C - Psycho_Coder - 06-16-2013

(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/html_node/Sleeping.html


RE: Timer in C - Boomslang - 06-16-2013

(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.


RE: Timer in C - noize - 06-16-2013

(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;
}

?


RE: Timer in C - Boomslang - 06-16-2013

(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.