Controlling Python application with C#
Hey, This issue was addressed before but not in this angle. I'm trying to control a Python application with C#. The application runs an unknown time and I need to hold the main C# application form until It "knows" when the Python a开发者_运维知识库pplication is done processing. I should mark that the Python Application has its own GUI which i'm trying to keep. So far, I've used:
ProcessStartInfo processStart= new System.Diagnostics.ProcessStartInfo(@app);
processStart.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
processStart.UseShellExecute = false;
processStart.RedirectStandardOutput = true;
processStart.CreateNoWindow = true;
Process myProcess = Process.Start(processStart);
Then I send a couple of "SendKey" methods including some TAB and ENTER. Furthermore, as you can probably infer from the code I'm trying to make the entire Python process hidden – I did succeed in open\close the Python application but didn't succeed in controlling it at all. Any suggestions?
Redirect stdin and the push characters in that way (rather than sendkeys)?
Have you looked into IronPython - it allows you to execute python code from within you .NET application natively.
SendKeys can only send keystrokes to the active application. Since you are forcing it to start without creating a window it can't receive the SendKeys.Send() messages. It sounds like what you really want is to use the functionality of the python code without presenting any UI from that application to your users.
You could approach this in one of 2 ways:
My first reccommendation is that you write a simple python script that imports the application you are trying to use, and invokes the functions inside the app that you need.
IF that won't work for you, you could allow the window to be created, then instantly set your application to be the foreground window using:
[DllImport("User32.dll")]
public static extern Int32 SetForegroundWindow(int hWnd);
public void ForceToFront()
{
SetForegroundWindow(Handle.ToInt32());
}
then you could send keys to the now background window using Win32 APIs. There's a pretty good (although fairly old) example here: http://www.codeproject.com/KB/cs/SendKeys.aspx
精彩评论