I'm not sure how to explain what I'm trying to ask so consider the following example:
int number;
void ChangeNumber()
{
number++;
UseNumber(number);
}
void UseNumber(int value)
{
// use number
}
I'm using this simple example to try to find out if when scripts are ran, it waits for one task to be completed before moving onto the next. For example, is UseNumber(number) only called once the number has been incremented before it?
Consider instead of number++ I was making a call to another method which did some complex calculations and also made it's own calls to other methods, would UseNumber(number) still wait for all that to run before being called? If not, would a callback be a good option for that situation to ensure previous tasks are completed first?
Delay in C: delay function is used to suspend execution of a program for a particular time. Declaration: void delay(unsigned int); Here unsigned int is the number of milliseconds (remember 1 second = 1000 milliseconds). To use delay function in your program you should include the "dos.
Time delay loops are often used in programs. These are loops that have no other function than to kill time. Delay loops can be created by specifying an empty target statement.
To delay a function call, use setTimeout() function. functionname − The function name for the function to be executed. milliseconds − The number of milliseconds. arg1, arg2, arg3 − These are the arguments passed to the function.
The sleep() function in python's time module is used to suspend the execution of a program for the given number of seconds.
The short answer is: C# behaves sensibly; you are guaranteed that the effects of earlier statements are observed by later statements.
The long answer is: things are more subtle than that when you look at more complex situations.
First off: C# does not guarantee that the order in which code actually runs is the same as the order in which it occurs in the program. The guarantee that is made is more subtle than that: the language guarantees that the program will appear to run as though it ran in the order the code occurred in the program.
This subtlety might need some more explanation. Suppose you had:
int foo = 1;
int bar = 2;
foo += 1;
bar += 1;
Console.WriteLine(foo);
Console.WriteLine(bar);
This program must increment foo and bar before the console writes, and the console writes must happen foo, then bar. However, the behaviour of the program fragment above is the same as:
int foo = 1;
int bar = 2;
bar += 1;
foo += 1;
Console.WriteLine(foo);
Console.WriteLine(bar);
And that has the same behaviour as this program fragment:
Console.WriteLine(2);
Console.WriteLine(3);
Since the observed side effects are identical in each case, C# is permitted to run any of these programs instead of the code as originally written.
Things then get a lot more complicated when you add:
The restrictions on how C# promises to preserve effect ordering get quite complicated when you throw those in; describing any of those could be an extremely long answer of its own, so if you have questions, try asking a second, more focussed question.
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