Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a delay in between statements in C#?

Tags:

c#

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?

like image 540
Kurtis Avatar asked Jan 15 '20 17:01

Kurtis


People also ask

Is there a delay function in C?

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.

What is a delay loop?

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.

How do you write a delay function?

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.

Which programming statement should be used to pause for 10 seconds?

The sleep() function in python's time module is used to suspend the execution of a program for the given number of seconds.


1 Answers

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:

  • Iterator blocks
  • Asynchronous workflows
  • Concurrency
  • Unmanaged code interop
  • Finalization

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.

like image 101
Eric Lippert Avatar answered Oct 09 '22 21:10

Eric Lippert