C# 如何打开一个进程并使steam读写器可供全班使用
我目前正在编写一个应用程序,该应用程序操纵已经构建的现有控制台应用程序。目前,我能够启动现有的应用程序,然后写入控制台并接收输出。但我需要我的应用程序基本上保持控制台应用程序在后台运行,并保持应用程序打开,准备向窗口写入新命令以接收更多信息。下面是我正在使用的当前代码。我想知道是否有一种方法可以在启动时调用此代码来启动控制台应用程序 代码:C# 如何打开一个进程并使steam读写器可供全班使用,c#,process,stream,console,C#,Process,Stream,Console,我目前正在编写一个应用程序,该应用程序操纵已经构建的现有控制台应用程序。目前,我能够启动现有的应用程序,然后写入控制台并接收输出。但我需要我的应用程序基本上保持控制台应用程序在后台运行,并保持应用程序打开,准备向窗口写入新命令以接收更多信息。下面是我正在使用的当前代码。我想知道是否有一种方法可以在启动时调用此代码来启动控制台应用程序 代码: private void Button_Click_1(object sender, RoutedEventArgs e) {
private void Button_Click_1(object sender, RoutedEventArgs e)
{
string ApplicationPath = "python";
string ApplicationArguments = "Console/dummy.py";
string returnValue;
//Process PyObj = new Process();
ProcessStartInfo PyObjStartInfo = new ProcessStartInfo();
PyObjStartInfo.FileName = ApplicationPath;
PyObjStartInfo.Arguments = ApplicationArguments;
PyObjStartInfo.RedirectStandardInput = true;
PyObjStartInfo.RedirectStandardOutput = true;
PyObjStartInfo.UseShellExecute = false;
//PyObjStartInfo.CreateNoWindow = true;
//PyObj.StartInfo = PyObjStartInfo;
Thread.Sleep(5000);
using (Process process = Process.Start(PyObjStartInfo))
{
StreamWriter sw = process.StandardInput;
StreamReader sr = process.StandardOutput;
if (sw.BaseStream.CanWrite)
{
sw.WriteLine("auth");
}
sw.Close();
sw.Close();
returnValue = sr.ReadToEnd();
MessageBox.Show(returnValue.ToString());
}
//Thread.Sleep(5000);
//PyObj.WaitForExit();
//PyObj.Close();
}
import os, pprint
def main():
keepGoing = True
while keepGoing:
response = menu()
if response == "0":
keepGoing = False
elif response == "auth":
print StartAuthProcess()
elif response == "verify":
print VerifyKey(raw_input(""))
elif response == "get":
print Info()
else:
print "I don't know what you want to do..."
def menu():
'''
print "MENU"
print "0) Quit"
print "1) Start Autentication Process"
print "2) Verify Key"
print "3) Get Output"
return raw_input("What would you like to do? ")
'''
return raw_input();
def StartAuthProcess():
return 1;
def VerifyKey(key):
if(key):
return 1;
else:
return 0;
def Info():
info = "{dummy:stuff}";
return info;
main()
正如您所看到的,这目前利用了一个按钮单击,但我希望代码在我的应用程序启动后立即运行。然后让控制台应用程序运行并保存在内存中,以便我可以与之交互。在C#net中有什么方法可以做到这一点吗
供参考。I cam调用的控制台应用程序为空,目前只返回虚拟答案。。下面是Python代码
python代码:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
string ApplicationPath = "python";
string ApplicationArguments = "Console/dummy.py";
string returnValue;
//Process PyObj = new Process();
ProcessStartInfo PyObjStartInfo = new ProcessStartInfo();
PyObjStartInfo.FileName = ApplicationPath;
PyObjStartInfo.Arguments = ApplicationArguments;
PyObjStartInfo.RedirectStandardInput = true;
PyObjStartInfo.RedirectStandardOutput = true;
PyObjStartInfo.UseShellExecute = false;
//PyObjStartInfo.CreateNoWindow = true;
//PyObj.StartInfo = PyObjStartInfo;
Thread.Sleep(5000);
using (Process process = Process.Start(PyObjStartInfo))
{
StreamWriter sw = process.StandardInput;
StreamReader sr = process.StandardOutput;
if (sw.BaseStream.CanWrite)
{
sw.WriteLine("auth");
}
sw.Close();
sw.Close();
returnValue = sr.ReadToEnd();
MessageBox.Show(returnValue.ToString());
}
//Thread.Sleep(5000);
//PyObj.WaitForExit();
//PyObj.Close();
}
import os, pprint
def main():
keepGoing = True
while keepGoing:
response = menu()
if response == "0":
keepGoing = False
elif response == "auth":
print StartAuthProcess()
elif response == "verify":
print VerifyKey(raw_input(""))
elif response == "get":
print Info()
else:
print "I don't know what you want to do..."
def menu():
'''
print "MENU"
print "0) Quit"
print "1) Start Autentication Process"
print "2) Verify Key"
print "3) Get Output"
return raw_input("What would you like to do? ")
'''
return raw_input();
def StartAuthProcess():
return 1;
def VerifyKey(key):
if(key):
return 1;
else:
return 0;
def Info():
info = "{dummy:stuff}";
return info;
main()
有几个地方可以放置可以立即运行的代码。首先,您将看到一个Program.cs,它具有
static void Main
函数。这就是应用程序开始执行的地方。直到调用Application.Run()
,表单才会显示。这是一个放置非常早期初始化内容的好地方
如果希望在首次打开表单时发生情况,可以重写虚拟方法:
请注意,您确实不应该在GUI线程(即您的按钮单击处理程序)中使用任何阻塞调用,如
Sleep
。这将导致GUI挂起,并感觉无响应。我不确定您计划如何与后台进程交互(它是自动的还是用户驱动的?),但是任何阻塞调用(即从stdout读取)都应该在后台线程上发生。然后,您可以使用Control.Invoke
将调用封送回UI线程,以更新控件等。您不能只启动进程并让它运行吗?(另外,我删除了wpf标记,因为问题中没有任何内容与wpf相关。)