How to Wait Until an Operation Finishes?

wait-job
powershell wait for all jobs to finish
powershell wait for cmdlet to finish
invoke-command wait until finished
swift dispatch group
powershell wait for process to finish
receive-job
powershell invoke-command wait for completion

I have C# code which included Python code in which it is run through CMD codes in C#. When the Python code is run the operations are done, a JSON file is created and then it will be opened in C#. In this situation, how the C# code can wait to check if the output of Python (data.json) is created or not, and just when the output is created, the rest of C# code is allowed to be run:

Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.Start();
process.StandardInput.WriteLine("F:\\");
process.StandardInput.WriteLine("cd F:\\Path");
process.StandardInput.WriteLine("python Python_Code.py");


process.StandardInput.Flush();
process.StandardInput.Close();
process.WaitForExit();

Then, the generated data with Python will be retrieved:

string Output_Python = File.ReadAllText(@"Data.json");
JavaScriptSerializer Ser = new JavaScriptSerializer();
Predicted Output = Ser.Deserialize<Predicted>(Output_Python);

You don't need to go through cmd.exe. The Python interpreter itself is an executable; in other words, it can be started and executed directly. The arguments for the Python interpreter (like the path+name of the script to be executed) and desired working directory can be set through the appropriate Process.StartInfo properties:

Process process = new Process();
process.StartInfo.FileName = "python.exe";
process.StartInfo.Arguments = "Python_Code.py";
process.StartInfo.WorkingDirectory = @"F:\Path";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.Start();

Now you only need to wait for the Python interpreter to exit (which means it finished executing the python script)

process.WaitForExit();

and after the Python process has exited, simply check if the json file exist/has been written:

if (System.IO.File.Exists(pathToJsonFile))
{
    ... do stuff with json file ...
}
else
{
    ... json file does not exist, something went wrong...
}

Side note: I kept process.StartInfo.RedirectStandardOutput = true; in my code example here, since i don't know what your program will really do. However, unless your program wants to process the output of the script that normally appears in a console window, setting RedirectStandardOutput to true is not necessary.

addOperations:waitUntilFinished:, wait. If YES , the current thread is blocked until all of the specified operations finish queue at a time and cannot be added if it is currently executing or finished. Make Excel VBA wait until operation is finished. Ask Question Asked 3 years, 3 months ago. Active 4 months ago. Viewed 4k times 0. I have made a macro in VBA to split

You should have a look at the FileSystemWatcher class. Documentation here.

Then you can do something like this:

using (FileSystemWatcher watcher = new FileSystemWatcher())
{
    watcher.Path = YourDirectory;
    // Watch for changes in LastWrite time
    watcher.NotifyFilter = NotifyFilters.LastWrite;

    // Watch for the wanted file
    watcher.Filter = "data.json";

    // Add event handlers.
    watcher.Created += WhateverYouWantToDo;
}

Swift: Easy way to wait for multiple background tasks to finish, The DispatchGroup basically functions like a kind of counter of running operations. You call the method enter() to signal that task begun and then� Prevent websites, ISP, and other parties from tracking you. Private Internet Access is a VPN that can prevent your Internet Service Provider, the government, and third-parties from tracking your online and allow you to stay completely anonymous.

You can check to see if the file data.json is finished being written to its output folder (Code from this answer):

private bool IsFileLocked(FileInfo file) 
{ 
   FileStream stream = null; 
   try 
  { 
    stream = file.Open(FileMode.Open,    FileAccess.ReadWrite, FileShare.None); 
  } 
  catch (IOException)
  { 
//the file is unavailable because it is: 
//still being written to 
//or being processed by another thread 
//or does not exist (has already been processed) 
    return true; 
  }
  finally 
  { 
    if (stream != null) stream.Close(); 
  } 
//file is not locked return 
return false; 
}

Wait-Job (Microsoft.PowerShell.Core), The Wait-Job cmdlet waits for PowerShell background jobs to finish before it displays the command prompt. You can wait until any background job is complete,� How to Wait Patiently. Everyone has to wait in queues, for deliveries, for exam results, and for all sorts of other things. Feeling impatient is a perfectly normal response to a situation, but you shouldn't allow that impatience to

Asynchronous Programming � The Julia Language, Basic Task operations This task will wait for five seconds, and then print done . The wait function blocks the calling task until some other task finishes. So for� Supplied delegate will be executed each frame after script MonoBehaviour.Update and before MonoBehaviour.LateUpdate. When the delegate finally evaluates to true, the coroutine will proceed with its execution. See Also: AsyncOperation, WaitForEndOfFrame, WaitForFixedUpdate, WaitForSeconds, WaitForSecondsRealtime, WaitWhile.

Block execution and wait for condition - MATLAB waitfor, This MATLAB function blocks statements from executing until the specified object closes (is deleted). Wait(Int32) is a synchronization method that causes the calling thread to wait for the current task instance to complete until one of the following occurs: The task completes successfully. The task itself is canceled or throws an exception.

Waiting for Coroutines in Kotlin, Further Job operations will be explored in future posts. Waiting for a Coroutine. To wait for a coroutine to finish, you can call Job.join . join is a� You'll want to use the wait command to do this for you. You can either capture all of the children process IDs and wait for them specifically, or if they are the only background processes your script is creating, you can just call wait without an argument.

Comments
  • Can you share your code?
  • You only need to do minimal adjustments of how you call your python script: Use the Process class to start the Python interpreter (python.exe) directly without going through cmd.exe. (Don't forget to set the working directory f:\path in the process.StartInfo and set the path+name of your python script as command line arguments in process.StartInfo as well). Then you can easily wait for python exiting (which itself will be an indication that the python script has finished and then check if the file is there or not...)
  • Dear @elgonzo. Unfortunately, I am so naive in C#. Could you let me know how to modify my code?
  • @AminSh, see my answer. It should give you an idea of how to utilize the Process class and the properties of Process.StartInfo...
  • Dear @elgonzo. Thank you so much. Could you help me how to force that the Python installed through Anaconda to be run? Because I have installed some packages, I need to run it by Python in Anaconda.
  • @AminSh, i don't know anything about Anaconda. Anyway, if you have a question/problem that is related to Anaconda, please ask a new question that specifically describes that problem...