Avatar of Tagyourareit
Tagyourareit
 asked on

c# while loop ... wait till code inside is executed before next loop

Hello,

i have the following probem. I have a loop statement which calls a method but
loop is executing before code in side is finished.

Following example:

   class Program
    {
        static void Main(string[] args)
        {
            Print();
            Console.ReadLine();
        }
        
        public static void Print()
        {
			int i = 10, j = 10;

			while(1>0)
			
            {
				calculate(i,j);
				
			}
                 
        }
		
		public static int calculate(int i, int j)
		{
			int b = i * j;
			Console.WriteLine("Result: " + b);
			
			return b;
		}

    }
		

Open in new window


Since i can not post my whole project here this example should do it.

In my case what happens is While loop executes and keeps on running
while code that "calculates" doesnt get executed.

Now i was wondering is there a way to stop oop execution will function which is
inside the loo is executed?

thank you!
C#

Avatar of undefined
Last Comment
Éric Moreau

8/22/2022 - Mon
ste5an

It's printing 100. I'm quite sure that this is the correct result of 10*10.

What do you expect?
Tagyourareit

ASKER
You cearly didnt read my question.
ste5an

i have the following probem. I have a loop statement which calls a method but
 loop is executing before code in side is finished.

This is clearly not true. Please describe what you expect.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Tagyourareit

ASKER
Since i can not post my whole project here this example should do it.

In my case what happens is While loop executes and keeps on running
while code that "calculates" doesnt get executed.

Now i was wondering is there a way to stop loop execution with function which is
inside the loop is executed?
ste5an

Your calculate method gets executed, otherwise you wouldn't have any output. Your loop has no terminating criteria, thus it runs for forever and prints 100.

So again: What do you expect? Why do you think, your method gets not executed?
ste5an

btw, the complete code I've used already before my first answer:

namespace Console_CS
{
    using System;

    class Program
    {
        static void Main(string[] args)
        {
            Print();
            Console.WriteLine("Done.");
            Console.ReadLine();
        }

        public static void Print()
        {
            int i = 10, j = 10;
            while (1 > 0) { calculate(i, j); }
        }

        public static int calculate(int i, int j)
        {
            int b = i * j;
            Console.WriteLine("Result: " + b);
            return b;
        }
    }
}

Open in new window


and its output:

Capture.PNG
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ste5an

And to make it just clear: I READ YOUR MESSAGE. MAYBE I DIDN'T UNDERSTAND IT. BUT THAT'S WHY I ASKED FOR CLARIFICATION!!!!!!!!!!!!
AndyAinscow

I agree with ste5an, whatever your problem is in the real case you have NOT reproduced it in your example code.

>>In my case what happens is While loop executes and keeps on running
while code that "calculates" doesnt get executed.

If you put a breakpoint inside the function (select a line with the cursor, press the F9 key and you should see a red dot appear to the left) and run the code.  When that line of code is reached your program will stop and you will be in the editor at that line where you can perform debugging actions.
ASKER CERTIFIED SOLUTION
Éric Moreau

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.