wait only for 1 thing

Hello there, i wanna know if there's a command to wait for example 20 seconds to do a command, not to pause the program, to do a thing only once at 20 seconds, the other things can run with no pauses, i hope you're understanding me. Thanks in advance.

1
2
3
4
5
6
a = 0;
while(1)
{
a =+ 1;
*thatcommand* (once at 20 sec to do: a =-10, the other things are going normaly)
}
For windows, you can use Sleep command.

1
2
3
4
5
6
7
8
9
10
11
12
#include<Windows.h>
#include<iostream>

int main(int argc, char ** argv)
{
    std::cout<<"Waiting 2 seconds";
    Sleep(2000); // the argument is in miliseconds
    
    return EXIT_SUCCESS;
}

I dont want to use sleep because it will pause the whole program
Then use a thread:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<Windows.h>
#include<thread>
#include<iostream>

void fir()
{
     /*you may want to insert a loop here*/
     Sleep(2000);
     std::cout<<"Waiting 2 seconds at a time";
}

int main(int argc, char ** argv)
{
    std::thread t1(fir);
    t1.detach();

    while(condition)
    {
       /*do other stuff while the thread works in the background*/
    }

    t1.join();
    return EXIT_SUCCESS;
}



There's also the sleep_for function for threads
 
std::this_thread::sleep_for(std::chrono::seconds(2));


Last edited on
Idk how to use it, it didnt worked too, there's my FULL problem:

I try to make an snake game and idk how to automatly move at all 1 s, here's my code:

(problem at 166, lmv is an variable to remember what was the last direction, the auto move is based on a if for it, but i dont know how to continue, please help me)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <iostream>
#define  _WIN32_WINNT 0x0500 //Win 2000 or later
#include <conio.h>           //By CosminPerRam
#include <stdlib.h>
#include <windows.h>
#include <unistd.h>
#include <string>
#include <ctime>            //TODO:
#include <cstdlib>          //2)User character to leave a trail in passed cords
                            //3)Levels
                            //4)Auto-Move
using namespace std;

short int x, y, xp, yp, av = 0, po = 0, gtv, fmp, fcx, fcy, sps, fcxr, fcyr, rw = 425, rh = 260, ssrw, ssrh, lmv;
string pre, gt;
char ch, p;

int reterr()
{
    getch();
    system("cls");
    cout<<"Fatal Error, application may crash!"<<endl;
    return 0;
}

int main()
{
    ::SendMessage(::GetConsoleWindow(), WM_SYSKEYDOWN, VK_RETURN, 0x20000000);

    //presets
    HWND console = GetConsoleWindow();
    RECT r;                                 //resolution things
    GetWindowRect(console, &r);
    MoveWindow(console, r.left, r.top, rw, rh, TRUE);

    srand(time(0));

    //start
    cout<<"Welcome to Snake!"<<endl;
    cout<<"(You can exit by pressing the ESCAPE button!)"<<endl;
    cout<<"Choose the type of gameplay!"<<endl;
    cout<<"Level (L) - Free (F): ";
    cin >> gt;

    system("cls");
    if(gt == "level" || gt == "Level" || gt == "L" || gt == "l" || gt == "LEVEL")
    {
        cout<<"Level-run selected!"<<endl;
        gtv += 2;
    }

    else if(gt == "free" || gt == "Free" || gt == "F" || gt =="f" || gt == "FREE")
    {
        cout<<"Free-run selected!"<<endl;
        gtv += 1;
    }
    else{reterr();}

    getch();
    system("cls");
    cout<<"Simple (S) - Advanced (A)"<<endl;
    cout<<"Choose your start-up setting type: ";
    cin >> pre;

    if(pre =="s" || pre =="S" || pre =="Simple" || pre =="simple" || pre =="SIMPLE")
    {
        system("cls");
        cout<<"Enter your character: ";
        cin >> ch;

        p = '+';
        fcx= 25;
        fcy= 13;
        fmp= 10;

        if(gtv == 2)
        {
            fcx= 13;
            fcy= 25;
        }

        system("cls");
    }

    else if(pre =="a" || pre =="A" || pre =="Advanced" || pre =="advanced" || pre =="ADVANCED")
    {
        system("cls");
        cout<<"Enter your character: ";
        cin >> ch;
        system("cls");
        cout<<"Enter the point character: ";
        cin >> p;
        system("cls");

        cout<<"Value of max points: ";
        cin >> fmp;
        system("cls");

        if(gtv == 1)
        {
            while(sps == 0)
            {
                cout<<"How high your last point want to be?(x cord): ";
                cin >> fcx;

                if(5 >= fcx) {cout<<"Need to be at least !"<<endl;}
                if(fcx >= 6) {sps =+ 1;}
                if(fcx <= 15){ssrw =+ 1;}
            }
            sps = 0;

            while(sps == 0)
            {
                cout<<"How high your last point want to be?(y cord): ";
                cin >> fcy;
                if(5 >= fcy) {cout<<"Need to be at least 6!"<<endl;}
                if(fcy >= 6) {sps =+ 1;}
                if(fcy <= 15){ssrh =+ 1;}
            }
            sps = 0;
            system("cls");
        }
    }

    else{reterr();}

    if(gtv == 2)
    {
        cout<<"Not developed already!"<<endl;

        //continue here m8!

        return 0;
    }

    else if(gtv == 1)
    {
        rw = fcx * 10.5;
        if(ssrw == 1){rw = fcx *20;}
        rh = fcy * 18.5;
        if(ssrh == 1){rh = fcy *25;}
        MoveWindow(console, r.left, r.top, r.right - r.left, r.bottom - r.top, FALSE);
        MoveWindow(console, r.left, r.top, rw, rh, FALSE);

        fcxr = fcx - 1;
        fcyr = fcy - 1;
        system("cls");

        while(1)
        {
            while(xp == 0 || yp == 0)
            {
                xp=rand()%fcxr;
                yp=rand()%fcyr;
            }

            av=getch();

            if(av==27)
            {
                system("cls");
                cout<<"Points: "<<po<<endl;
                return 0;
            }

            if(lmv == 0;){ How to do it?}
            if(lmv == 1;){}
            if(lmv == 2;){}
            if(lmv == 3;){}

            if(av==72){y = y - 1; lmv = 0;} //UP
            if(av==80){y = y + 1; lmv = 1;} //DOWN
            if(av==75){x = x - 1; lmv = 2;} //LEFT    //keys
            if(av==77){x = x + 1; lmv = 3;} //RIGHT

            if(x==0){x = 1;}        //Protection for border //begin
            if(y==0){y = 1;}
            if(x==fcx){x = fcxr;}      //Protection for border //end
            if(y==fcy){y = fcyr;}

            system("cls");

            COORD coord = {x, y};
            SetConsoleCursorPosition (GetStdHandle (STD_OUTPUT_HANDLE), coord);
            cout<<ch;

            COORD coord2 = {xp, yp};
            SetConsoleCursorPosition (GetStdHandle (STD_OUTPUT_HANDLE), coord2);
            cout<<p;

            if(x == xp && y == yp)
            {
                po += 1;

                xp=rand()%fcxr;
                yp=rand()%fcyr;
                if(fmp == po)
                {
                    system("cls");
                    cout<<"Max points ("<<fmp<<") has been reached, congratulations!"<<endl;
                    return 0;
                }
            }
        }
    }
    else{reterr();}

    return 0;
}


I also didnt really want the full answer, just some help to do it. Thanks in advance!
Last edited on
closed account (48T7M4Gy)
http://stackoverflow.com/questions/158585/how-do-you-add-a-timed-delay-to-a-c-program
+kemort i said above this will not work
If you want to run multiple blocks of code in the same, you use threads. But for a simple console snake game you won't need them, you can do everything in a linear way. Rethink your code, the whole part after 166 could use a switch rather than a mire of ifs. Use an array to represent your snake (as it grows in size), track the board and positions on a 2d array, it will be much more simple that way.

short int x, y, xp, yp, av = 0, po = 0, gtv, fmp, fcx, fcy, sps, fcxr, fcyr, rw = 425, rh = 260, ssrw, ssrh, lmv;

For the love of God don't do this. It's sore to the eye, clogs the code and you don't even need all of those to be global.

Also, using system() is a bad habit, try to grow out of it.
Ok, i'l re-do the snake, but what else i can use to delete the screen text? like system("cls") is doing

EDIT: Also, i still wonder myself how to make it to auto move when idle and move whenever user want
Last edited on
closed account (48T7M4Gy)
+kemort i said above this will not work

Don't stamp your foot too hard Cosmin, it might fall off. Two people now have posted advice and pointers on this and the reason it doesn't work for you is because I think you might be assuming that your sleep() is the same as the one being referred to by us - which do in fact work.

See also, http://en.cppreference.com/w/cpp/thread/sleep_for plus associated references and executable samples.

The reason you can't get it to work is you either don't or can't understand what's going and/or because your system is not running C++11 or C++14 in which case I can imagine it is very annoying and frustrating.

Good luck with it though and be assured there is no obligation with the advice :)
So, you want to wait some time (e.g. 20 seconds) before executing a command, but at the same time do other things?
closed account (48T7M4Gy)
Yep
Cosmin writes

1. a command to wait for example 20 seconds to do a command,
2. not to pause the program,
3. to do a thing only once at 20 seconds,
4. the other things can run with no pauses,
... i hope you're understanding me.
Last edited on
+kemort you right, i managed at a finale to make it work, thanks!
closed account (48T7M4Gy)
Good on you Cosmin. I'm glad it worked for you mate. You might like to know I learned something too. I had trouble trying the samples out on my machine until I realised the 14/11 problem.

Now all I have to do is find an application. Look out for snakes! :)
Topic archived. No new replies allowed.