How to run a while loop, while another function sleeps.

Hello,
I want to know how to run a while loop in main, while another function is executing a task each 20 sec.

Right now I have this code

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
#include <iostream>
#include <windows.h>
#include <Audioclient.h>
#include <endpointvolume.h>
#include <mmdeviceapi.h>


BOOL ChangeVolume(float nVolume)
{
	HRESULT hr = NULL;
	IMMDeviceEnumerator *deviceEnumerator = NULL;
	hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER,
		__uuidof(IMMDeviceEnumerator), (LPVOID *)&deviceEnumerator);
	if (FAILED(hr))
		return FALSE;

	IMMDevice *defaultDevice = NULL;
	hr = deviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &defaultDevice);
	deviceEnumerator->Release();
	if (FAILED(hr))
		return FALSE;

	IAudioEndpointVolume *endpointVolume = NULL;
	hr = defaultDevice->Activate(__uuidof(IAudioEndpointVolume),
		CLSCTX_INPROC_SERVER, NULL, (LPVOID *)&endpointVolume);
	defaultDevice->Release();
	if (FAILED(hr))
		return FALSE;

	hr = endpointVolume->SetMasterVolumeLevelScalar(nVolume, NULL);
	endpointVolume->Release();

	return SUCCEEDED(hr);
}

void OpenFan() {

		Sleep(20000);
		ShellExecute(NULL, "open", "https://www.youtube.com/watch?v=A_af256mnTE", NULL, NULL, SW_SHOWNORMAL);


}

int main()
{
	while (true)
	{
			CoInitialize(NULL);
			ChangeVolume(0.2);
			CoUninitialize();
			OpenFan();
	}

	return 0;
}


I was just wondering how to keep the volume at 0.2 (20%) while the OpenFan(); function is running each 20 second.

Thanks, Neahle.
Last edited on
Call the function concurrently in another 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
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
#include <iostream>
#include <thread>
#include <chrono>
#include <functional>
#include <atomic>
#include <ctime>

// call the function periodically, once every 'period_in_milliseconds' milliseconds.
// till 'stop_calls' is set to true
void periodic_call( int period_in_milliseconds, std::atomic<bool>& stop_calls, std::function< void() > function )
{
    const auto period = std::chrono::milliseconds(period_in_milliseconds) ;

    while( !stop_calls )
    {
        // call the function
        auto start = std::chrono::steady_clock::now() ;
        function() ; // assumes that the function won't take more than 'period_in_milliseconds' milliseconds
        auto end = std::chrono::steady_clock::now() ;

        // set delta to the time (in milliseconds) that was used to execute the function
        const auto delta = std::chrono::duration_cast<std::chrono::milliseconds>(end-start) ;
        const auto sleep_time = period - delta ; // compute the time till next call
        std::this_thread::sleep_for(sleep_time) ; // sleep till it's time for the next call
    }
}

void open_fan()
{
    static int n = 0 ;
    auto t = std::time(nullptr) ;
    char now[128] ;
    std::strftime( now, sizeof(now), "%H:%M:%S : ", std::localtime(&t) ) ;
    std::cout << ++n << ". " << now << "open_fan "
              << "invoke ShellExecute( ... )\n" << std::flush ;
}

int main()
{
    // launch a thread to call open_fan once every 2 minutes (2000 millisecs * 60)
    // note: stop_calls is passed as a wrapped reference
    std::atomic<bool> stop_calls { false } ;
    std::thread periodic_call_thread( periodic_call, 2000*60, std::ref(stop_calls), open_fan ) ;

    // while the periodic call of open_fan are going on in the background,
    // do other things eg. change volume etc.

    std::cout << "press enter to stop the program.\n" ;
    std::cin.get() ;

    stop_calls = true ; // stop the calls
    periodic_call_thread.join() ; // wait for the thread to finish
    std::cout << "done!\n" ;
}
Thank you very much, Helped a lot. Just by any chance, do you know any good C++ books for beginners?
Honestly, the very best for beginners is the tutorial on this very site.

http://www.cplusplus.com/doc/tutorial/
> do you know any good C++ books for beginners?

This is a good introductory book:
'Programming: Principles and Practice Using C++, 2nd Edition' By Bjarne Stroustrup
https://www.amazon.com/Programming-Principles-Practice-Using-2nd/dp/0321992784

This too, is a good book: C++ Primer (5th Edition) by Lippman, Lajoie and Moo
https://www.amazon.com/Primer-5th-Stanley-B-Lippman/dp/0321714113

For more information on these books, and a few others, see
4. Learn More https://isocpp.org/get-started
What is the best book to learn C++ from? https://isocpp.org/wiki/faq/how-to-learn-cpp#best-book

The tutorial on this site is sadly inadequate in more ways than one. Get a good book (or two); you won't regret it.
Come on Mr. Borges,

The tutorial here is not bad for beginners. I have tried reading more in depth books, but they tend to get really complicated and I can't seem to understand what they are trying to teach. What good is that to a beginner?
I will look into some books and the tutorials on this site. Thanks a lot both of you.
Topic archived. No new replies allowed.