| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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. I'm Using MVC6 and my OS is Win2k. The program waits for incomming messages of sort.And It has to be a part of the same program. Any kind help, may it be psuedo code or links, will be much appreciated. regards, jaro |
|
#2
|
|||
|
|||
|
As far as i understand your idea, you want your code to run, and, while it's running a timer to run, and every x seconds some function to be called.
If that's the case, threads are what you need. There's a lot to be said about threads, you can find quite a lot about them on the net. Basically the idea is that a thread is another process that's running along with your main program and can share resources with it. So in your case you should start a thread with the timer and the calling of the function you want to execute every x seconds. Some pseudo-code: void do_program_logic(); void timer(void* passed_var); int main() { volatile int passed_value = 5; _beginthread(timer,0, &passed_value); do_program_logic(); return 0; } There's a lot to be learnt for threads - how they work, how to pass variables to the new formed thread, what's the volatile keyword, and so on, it's really a complex matter, but i hope that gives you the idea what you have to look for on the internet ![]() Hope that helps ![]() |
|
#3
|
|||
|
|||
|
Do we have a concept of threading in C??
|
|
#4
|
|||
|
|||
|
as far as i know it is fully supported, i think there are a lot of *nix applications using threads, written in c, but i'm not fully sure.
Later edit: I've cheked - it's fully supported, if your OS supports it. Unless you're writing for DOS or some other godforsaken os, it's supported. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Timed Function in C |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|