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!
TagyourareitAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

What do you expect?
0
TagyourareitAuthor Commented:
You cearly didnt read my question.
0
ste5anSenior DeveloperCommented:
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.
0
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

TagyourareitAuthor Commented:
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?
0
ste5anSenior DeveloperCommented:
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?
0
ste5anSenior DeveloperCommented:
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
0
ste5anSenior DeveloperCommented:
And to make it just clear: I READ YOUR MESSAGE. MAYBE I DIDN'T UNDERSTAND IT. BUT THAT'S WHY I ASKED FOR CLARIFICATION!!!!!!!!!!!!
0
AndyAinscowFreelance programmer / ConsultantCommented:
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.
0
Éric MoreauSenior .Net ConsultantCommented:
Your sample here is synchronous and it should never produce the behavior you are describing.

Is your real project using some kind of task/thread/async/... constructs?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.