Good day.

void FunctionZ() { (new Thread(delegate() { Console.Writeln(0); FunctionA(); Console.Writeln(2); })).Start(); } void FunctionA() { (new Thread(delegate() { Console.Writeln(1); })).Start(); } 

The problem is that when FunctionZ is executed, the end of the FunctionA function is not expected, and the code just goes on. And you must first wait for the end of the execution of the FunctionA function and only then continue the execution.

UPD:

I specify a little, when executing the code, 0 2 1 is often displayed at the top. Although 0 1 2 should be displayed. FunctionA is not expected to be executed, and the following code is executed immediately. And you need to be expected.

Closed due to the fact that off-topic participants Alexey Shimansky , zRrr , rdorn , Igor , Streletz Jun 19 '16 at 7:58 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • "The question is caused by a problem that is no longer reproduced or typed . Although similar questions may be relevant on this site, solving this question is unlikely to help future visitors. You can usually avoid similar questions by writing and researching a minimum program to reproduce the problem before publishing the question. " - Alexey Shimansky, zRrr, rdorn, Igor, Streletz
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    You can't have FunctionA running synchronously. Are you sure that Console.Writeln(2); Is your example in the right place? - VladD
  • You do not confuse? Language exactly C #? I’m not too lazy to send your code and it works correctly within the call sequence (not counting the non-existing Console.Writeln method). However, the following situation is possible: The FunctionZ () method returns control before the flow created in the method body is complete, respectively the FunctionZ () method is called repeatedly, then you can really observe the situation when 0 2 1. is displayed, but in this case the output of the 2-ki will belong to the thread that was created in the previous call rather than the one that output 0 and 1 - Alexey
  • Sure. This is a simplified example. It is clear that the code is not just output to the console. In FunctionA, code that runs in about 3-4 seconds. And it happens so that it has not finished yet, and so let's say 2 has already been displayed in the log. - LorDo
  • Another interesting point is if you take all the function code of the FunctionA and just cram it into place of the function call. That code works as it should. - LorDo
  • 3
    In the FunctionA function, is the code exactly executed sequentially, without creating new threads or an asynchronous call? If the FunctionA function is executed sequentially, then it simply cannot return control before it is executed. - Alexey

2 answers 2

After calling Start() , you start a new thread that lives its own life, it executes FunctionA To wait for this thread to perform at some point, you need to call the Join method

 void FunctionZ() { Thread thread = new Thread(delegate() { /* КакиС-Ρ‚ΠΎ дСйствия */ FunctionA(); /* КакиС-Ρ‚ΠΎ дСйствия */ }); thread.Start(); //ΠŸΠΎΡ‚ΠΎΠΊ запустился Π½Π° Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΈΠ΅ thread.Join(); //Π’ этой строкС ΠΆΠ΄Π΅ΠΌ ΠΎΠΊΠΎΠ½Ρ‡Π°Π½ΠΈΠ΅ Π΅Π³ΠΎ выполнСния } 
  • Slightly specified the problem. See the edit. - LorDo

You can use Join. And you can simply refuse to create a separate thread to perform the operations that need to wait.

  void FunctionZ() { /* КакиС-Ρ‚ΠΎ дСйствия */ FunctionA(); /* КакиС-Ρ‚ΠΎ дСйствия */ } 

If the creation of a separate thread was used by you to ensure that the UI is not β€œfrozen”, it would be wiser to use an asynchronous call in this case.

What to apply specifically in your case depends on many nuances of the project. You decide.

  • Slightly specified the problem. See edit - LorDo