2

私は現在、すでに構築されている既存のコンソールアプリを操作するアプリケーションを作成しています。現在、既存のアプリケーションを起動してから、コンソールに書き込んで出力を受け取ることができます。ただし、基本的にコンソールアプリをバックグラウンドで実行し続け、アプリを開いたままにして、ウィンドウに新しいコマンドを書き込んで詳細情報を受け取る準備をするために、アプリが必要です。以下は私が使用している現在のコードです。起動時にこのコードを呼び出してコンソールアプリケーションを起動する方法があるかどうか疑問に思っています。

コード

   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();
    }

ご覧のとおり、これは現在ボタンクリックを利用していますが、アプリケーションが起動したらすぐにコードを実行したいと思います。次に、コンソールアプリを実行し、メモリ内に置いて、操作できるようにします。C#.netでこれを行う方法はありますか?

参考のために。私が呼び出しているコンソールアプリケーションは空白で、とりあえずダミーの答えを返します。これが以下のPythonコードです。

Pythonコード

  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()
4

1 に答える 1

1

すぐに実行されるコードを配置できる場所がいくつかあります。まず、static void Main関数を含む Program.cs が表示されます。ここで、アプリケーションの実行が開始されます。への呼び出しまで、フォームは表示されませんApplication.Run()。これは、非常に初期の初期化を配置するのに適した場所です。

フォームが最初に開かれたときに何かをしたい場合は、仮想Form.OnShownメソッドをオーバーライドできます。

protected override void OnShown(EventArgs e) {
    base.OwnShown(e);

    // Call whatever functions you want here.
}

SleepGUI スレッド (ボタン クリック ハンドラーとも呼ばれます) のようなブロッキング呼び出しを使用しないでください。これにより、GUI がハングし、応答しなくなります。バックグラウンド プロセスとのやり取りをどのように計画しているか正確にはわかりません (自動でしょうか、それともユーザー主導でしょうか?)。Control.Invokeその後、呼び出しを UI スレッドにマーシャリングして戻し、コントロールなどを更新するために使用できます。

于 2013-03-22T05:37:39.327 に答える