I recently graduated college (yay!)! Unfortunately part of that degree was being forced to take 2 C++ classes, and 1 java programming class. I can't tell you how I hated those classes, gave myself headaches (literally, actual headaches) just trying to figure out the logic of some programs.
But all those forced classes, did make me start thinking about programs I would like to either have, or write. Some seem waaay too complex for C++, and some seem a might simpler.
My laptop takes 20 some odd minutes to boot nowadays, and part of that is the number of programs I have starting up when I login. While waiting for it to boot up today, I had a thought. This is an old thought, about a program with a simple purpose. But I'm not sure if its even possible, much less how to start it.
So my question is, can you start programs from a C++ program?
My goal is to put a program in my startup folder, that waits a certain period of time (say...10 minutes) then starts up programs one at a time. Ideally, this program would wait till the programs are running before opening up any other programs, but I figured I'd start with a timer or something and expand it from there. So, can this be done? And, if so, then how?
While C++ is perfectly capable of doing this, a scripting language like Python or batch would be better.
I don't see the point, though. If you have to drive 10 km, driving them in 1 km bursts with 1 minute breaks doesn't decrease the total travel time.
Just out of curiosity, what did you graduate on if you only had 2 C++ and 1 Java classes, and apparently you didn't even get to system calls?
I specialized in networking. If you need help with Cisco routing and Switching, I could probobly help you out.
Programming 1 was basic stuff, writing to a cli screen, passing variables between functions (I think they're called functions) writing to a text document, ect. Programming 2 got more complex, though I couldn't tell you how (barely passed that one). And then had to move into java programming, because there was no Programming 3 class based off C++.
I'm not sure what your talking about with that driving analogy. But if you study PC repair and maintenance, then you would know, then less system resources being taken up at one time, the better it is for the machine in question. This is why you try to lessen the number of programs starting up.
If 1 program takes 10 min to load, then 3 of those programs might take 50 min to load, because all 3 are trying to use the same amount of resources at the same time. Its up to the OS, to delegate who gets access to what, in what order. normally this is first come first serve, so each program has to constantly ask for the resources, in hopes of getting access to them. As you can imagine, having multiple programs asking for the same resources, at the same time, can bog down everything.
So, if I only load the basics at first, then have a program to load programs with a time limit between them, those programs could get access to the resources they need, without much competition. So...viola, faster startup time. And less strain on the computer hardware.
Hmmm... That's an interesting theory, and I'm pretty sure C++ could handle that.
If you're using Windows, no double you could create a very simple program which has no Window / Console, and use the Timer, or even just Sleep for a while to launch other applications in a sequence.
I'd like to start this program...
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
Sleep(1000 * 60 * 10); // sleep for 10 minutes
system("ProgramA.exe"); // launch program A
Sleep(1000 * 60 * 2); // sleep for 2 minutes
system("ProgramB.exe"); // launch program B
// ... other program in sequence here
return 0;
}
Build this program, and put it into the Startup folder, it should work.
> Would that system("ProgramA.exe"); need the program path?
It will call the program A when the program A and your program are in the same directory (folder). If you need program path, use this : system("C:\\Program B.exe");
Lol, ok then. I was expecting to be alot more complicated then that. I was thinking I'd have to create a timer function, then call it whenever I needed to open a program and then delay. Then do some some other wacky function calls I didn't even know about.
I wasn't expecting it to be so simple.
I'll have to experiment with this when I get some free time. Working 2 12 hour days coming up, so I'll give it a shot on wed.
Fair enough. The program I'd be writing to open programs slowly, over a longer period of time, I'm going to call auto open. Nothing fancy about it, just that it opens programs automatically.
So, would that sleep function stop 'auto open' from running for the specified amount of time? Or would it stop the program being opened (program A in the code), for the specified amount of time?
Was that explained better?
I'm asking, because not many programs are built to freeze up on purpose, for long periods of time. And I'm worried that might cause instability and crashes.
So it freezes the custom program I'd be writing? Or it freezes the program I'd be opening?
It would freeze all execution on the program you would be writing, not the program you would be opening. Basically you can think of it as it will idle for that many milliseconds on that line of your program. It won't effect the program that you just opened earlier.
So for example
1 2 3 4 5 6 7 8 9 10 11 12
int main()
{
std::cout << "Hello!" << std::endl;
// Some code to open a program here
Sleep(1000); // Our program will idle on this line for 1 second before moving onto line 8
// Some code to open another program here.
return 0;
}
So again no it will not effect any other programs on your computer just the one that you wrote.
I'm asking, because not many programs are built to freeze up on purpose, for long periods of time. And I'm worried that might cause instability and crashes.
I wouldn't worry about it many programs actually do idle for long periods of time (At least for computers). Also don't think of it as freezing up your program, instead think of it as your program is going into idle mode for X amount of milliseconds. The only purpose of that Sleep() call is to give the program that you just opened from C++ some time to initialize like you wanted. Hopefully that makes sense.
One other thing instead of using the Sleep() function I would recommend you use std::this_thread::sleep_for(std::chrono::milliseconds(3000)); instead which is cross platform. The implementation of Sleep() is not standard so cross platform compatibility is not guaranteed.