Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Async Infinite Loop

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

like image 436
MigDinny Avatar asked Jan 09 '16 12:01

MigDinny


People also ask

Is it possible to create an infinite loop?

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.

What happens if we run infinite 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.

What is an example of an infinite loop?

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.


2 Answers

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.

like image 172
usr Avatar answered Sep 28 '22 06:09

usr


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.

like image 35
David Avatar answered Sep 28 '22 06:09

David