1

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);
double-beep
  • 5,031
  • 17
  • 33
  • 41
Amin
  • 127
  • 8
  • Can you share your code? – Stemado Feb 24 '19 at 19:17
  • 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...) –  Feb 24 '19 at 19:36
  • Dear @elgonzo. Unfortunately, I am so naive in C#. Could you let me know how to modify my code? – Amin Feb 24 '19 at 19:42
  • @AminSh, see my answer. It should give you an idea of how to utilize the Process class and the properties of Process.StartInfo... –  Feb 24 '19 at 19:53

3 Answers3

1

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.

  • 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. – Amin Feb 24 '19 at 20:55
  • @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... –  Feb 24 '19 at 20:59
0

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;
}
SBFrancies
  • 3,987
  • 2
  • 14
  • 37
0

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; 
}
Stemado
  • 599
  • 5
  • 10