Jump to content

C# Infinite Program

Fyfey96
Go to solution Solved by espurritado,

WHY??

 

you have an enormous memory leak, just do

 static void ContiniuousChecker()
        {
		while (true){
			//Do stuff here

			Thread.Sleep(500);
            }
                                                   
         }

Hello,

 

I have made a c# program to look at an xml file and move the data and do what i wanna do with it. I need the program to run constant. I have it working but sometimes it crashes randomly with error "System.StackOverflowExcpetion" Make sure you dont have an infinite loop. I have it working like this. Any ideas on what could be causing crash at random times. Thanks.

 

 static void ContiniuousChecker()
        {

			//Do stuff here

			Thread.Sleep(500);
            ContiniuousChecker();
                                                   
         }

 

Link to comment
Share on other sites

Link to post
Share on other sites

WHY??

 

you have an enormous memory leak, just do

 static void ContiniuousChecker()
        {
		while (true){
			//Do stuff here

			Thread.Sleep(500);
            }
                                                   
         }

The best way to measure the quality of a piece of code is "Oh F*** "s per line

Link to comment
Share on other sites

Link to post
Share on other sites

I see that you already have the answer, but remember that when using recursion you should also return. When you call a function memory is allocated for that function, it allows the function to use it again when it returns. If you keep calling new functions indefinitely, all of the memory will be consumed. So remember to only use recursion when you intend to return.

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, espurritado said:

WHY??

 

you have an enormous memory leak, just do


 static void ContiniuousChecker()
        {
		while (true){
			//Do stuff here

			Thread.Sleep(500);
            }
                                                   
         }

Thanks, I thought of trying this just as i posted this and no isses for last 190Mins will see how it goes thanks.

 

2 hours ago, Ekras said:

I see that you already have the answer, but remember that when using recursion you should also return. When you call a function memory is allocated for that function, it allows the function to use it again when it returns. If you keep calling new functions indefinitely, all of the memory will be consumed. So remember to only use recursion when you intend to return.

I dont know if i need to return because i dont need anything else done apart from this?

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, Fyfey96 said:

Thanks, I thought of trying this just as i posted this and no isses for last 190Mins will see how it goes thanks.

 

I dont know if i need to return because i dont need anything else done apart from this?

Every time you call a function, the address to return to is stored on the stack. This way, when the function is done, it can get that address from the stack in order to continue execution on the next line after the function call.

 

In your original code, the function "ContiniuousChecker" calls itself over and over again without ever returning. That means return addresses are endlessly put on the stack but never taken off, until the stack overflows. All you need is a endless loop like @espurritado demonstrated.

Link to comment
Share on other sites

Link to post
Share on other sites

9 minutes ago, Unimportant said:

All you need is a endless loop like @espurritado demonstrated.

You don't even need a loop...

using System;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var watcher = new FileSystemWatcher(Directory.GetCurrentDirectory(), "test.txt") { EnableRaisingEvents = true };

            watcher.Changed += (o, a) => Console.WriteLine($"{a.FullPath} {a.ChangeType}");

            Console.ReadKey();
        }
    }
}

 

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

13 hours ago, Unimportant said:

Every time you call a function, the address to return to is stored on the stack. This way, when the function is done, it can get that address from the stack in order to continue execution on the next line after the function call.

 

In your original code, the function "ContiniuousChecker" calls itself over and over again without ever returning. That means return addresses are endlessly put on the stack but never taken off, until the stack overflows. All you need is a endless loop like @espurritado demonstrated.

Brilliant thanks been running for 18 Hours now and no issues anymore :)

Link to comment
Share on other sites

Link to post
Share on other sites

On 12/3/2016 at 7:28 PM, Voze said:

Thanks for making my eyes bleed with that indentation.

I dont know what caused that but when i added to this it keeps moving over. It annoys me too. I always like my indents correct haha

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×