I'm programming a C# software and it has some functions which make like a check-in on my server 5 in 5 seconds (they send http request, but it doesn't matter for my problem). Here is the code:
log();
while (true)
{
alive(); // this sends an http request to my server
Thread.Sleep(5000); // sleep for 5 seconds avoiding computer overcharge
}
MessageBox.Show("after infinite loop"); // this is never executed but i pretend to do so
So, my problem is: I got an infinite loop and after that infinite loop I want to write ANOTHER infinite loop which runs asynchronously and does something each 100ms. Of course "that something" i cannot write it inside the existing loop because that loop is executed 5 in 5 seconds. I got to code a lot of infinite loops like those, each one being executed independently from another (or asynchronously) with different times.
I have searched a lot and my conclusion is to write every loop asynchronously. My question is: how to do that? Every code I found was completely incomprehensible for me. I simply couldn't understand them. So, if you have a better option than async infinite loops (which i think is the best option indeed), you can speak about it, otherwise i need some help coding this async infinite loops...
Sorry for my bad english, thanks in advance
We can create an infinite loop through various loop structures. The following are the loop structures through which we will define the infinite loop: for loop.
An infinite loop is a piece of code that keeps running forever as the terminating condition is never reached. An infinite loop can crash your program or browser and freeze your computer. To avoid such incidents it is important to be aware of infinite loops so that we can avoid them.
An infinite loop occurs when a condition always evaluates to true. Usually, this is an error. For example, you might have a loop that decrements until it reaches 0.
var loop1Task = Task.Run(async () => {
while (true) {
await Task.Delay(5000);
}
});
var loop2Task = Task.Run(async () => {
while (true) {
await Task.Delay(100);
}
});
Now you have two loops. You can add code to them.
and after that infinite loop...
That's not really what "infinite" means :)
You can have two simultaneous running processes, simply put them in their own threads. This would also have the added benefit that your application host isn't blocked by your loop, so you'd be free to do other things with the application host. (And it doesn't appear to be "frozen" to the host system.)
It could be something as simple as this...
public class Worker
{
public void DoWork()
{
while (true)
{
alive(); // this sends an http request to my server
Thread.Sleep(5000); // sleep for 5 seconds avoiding computer overcharge
}
}
}
Then to start it as a thread:
var workerObject = new Worker();
var workerThread = new Thread(workerObject.DoWork);
workerThread.Start();
You can send a variety of information and signals to threads to provide them with data from the consuming code and to control them while they're running. (Since you certainly wouldn't want runaway threads that you can't interact with.) So this is just a very simple example to get you started and illustrate the concept.
You can find a lot more information about and more complete examples on MSDN, among a number of other places.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With