-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
152 lines (126 loc) · 4.51 KB
/
Copy pathProgram.cs
File metadata and controls
152 lines (126 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Collections;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Collections.Generic;
namespace CSharpPython
{
class Program
{
const string _pythonScriptThatWilLGenerateAnError = "JustGarbage.py";
const string _pythonScriptToExecute = "Python101.py";
private static AutoResetEvent _doneHandlingSTDOUT = new AutoResetEvent(false);
private static AutoResetEvent _doneHandlingSTERR = new AutoResetEvent(false);
private static AutoResetEvent[] _allToWaitOn = { _doneHandlingSTDOUT, _doneHandlingSTERR };
private static void HandleSTDERR(
object sendingProcess,
DataReceivedEventArgs stderr)
{
// Empty stream so done handling standard error stream
if (String.IsNullOrEmpty(stderr.Data))
{
_doneHandlingSTERR.Set();
}
else
{
Console.Write("There was an error: ");
Console.WriteLine(stderr.Data);
}
}
private static void HandleSTDOUT(
object sendingProcess,
DataReceivedEventArgs stdout)
{
// Empty stream so done handling standard output stream
if (String.IsNullOrEmpty(stdout.Data))
{
_doneHandlingSTDOUT.Set();
}
else
{
Console.WriteLine(stdout.Data);
}
}
private static void RunPythonScript(string script, string scriptArgs)
{
ProcessStartInfo processStartInfo = new ProcessStartInfo()
{
Arguments = script + " " + scriptArgs,
FileName = "python",
UseShellExecute = false, // can only redirect STDIO when UseShellExecute=false
RedirectStandardOutput = true,
RedirectStandardError = true
};
using (Process process = new Process())
{
process.StartInfo = processStartInfo;
process.OutputDataReceived += HandleSTDOUT;
process.ErrorDataReceived += HandleSTDERR;
if (!process.Start())
{
Console.WriteLine("Process failed to start.");
return;
}
process.BeginErrorReadLine();
process.BeginOutputReadLine();
process.WaitForExit();
Console.WriteLine("Process exit code:" + process.ExitCode);
process.Close();
}
WaitHandle.WaitAll(_allToWaitOn);
}
private static void InvokePythonScript()
{
RunPythonScript(_pythonScriptToExecute, "Arg0 Arg1 Arg2");
RunPythonScript(_pythonScriptThatWilLGenerateAnError, "Arg0 Arg1 Arg2");
}
const string _pythonScriptToExecuteDirectly = "ProofByFile.py";
private static void RunScriptDirectly()
{
string filenameToCreate = DateTime.Now.ToString("yyyyMMddhhmmssfff") + ".txt";
ProcessStartInfo processStartInfo = new ProcessStartInfo()
{
Arguments = filenameToCreate,
FileName = _pythonScriptToExecuteDirectly,
};
using (Process process = new Process())
{
process.StartInfo = processStartInfo;
process.Start();
process.Close();
}
string commandLineOfExecutable =
Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]);
string fileCreatedByPython =
Path.Combine(commandLineOfExecutable, filenameToCreate);
bool fileFound = false;
for (int i = 0; i < 5; i++)
{
fileFound = File.Exists(fileCreatedByPython);
if (fileFound)
{
break;
}
Thread.Sleep(100);
}
if (fileFound)
{
Console.WriteLine("Python created file as expected.");
}
else
{
Console.WriteLine("Python did not create the file as expected.");
}
}
static void Main(string[] args)
{
RunScriptDirectly();
InvokePythonScript();
return;
}
}
}