AIR NativeProcess standardInput not connecting to app

后端 未结 2 1715
名媛妹妹
名媛妹妹 2021-01-05 17:37

I\'m writing an AIR app that launches a C# console application and they need to communicate. I\'d like to use standard input/standard output for this, however I can\'t seem

2条回答
  •  孤城傲影
    2021-01-05 17:48

    In case anyone else besides me still use Adobe AIR with C# console apps, here's a solution to the problem:

    Just like @tom-makin points out in the linked answer, you need to create another console app that runs on .NET 3.5, which then opens your newer .NET console app and passes input to it. Here's my take on such an app:

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Threading;
    
    namespace StdInPiper
    {
        class Program
        {
            static void Main(string[] args)
            {
                // Remove the first arg from args, containing the newer .NET exePath.
                string exePath = args[0];
                var tempArgs = new List(args);
                tempArgs.RemoveAt(0);
                string argsLine = "";
                foreach (string arg in tempArgs)
                {
                    argsLine = argsLine + " " + arg;
                }
                argsLine = argsLine.Trim();
    
                var process = new Process();            
                process.EnableRaisingEvents = true;
    
                process.StartInfo.UseShellExecute = false;            
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.RedirectStandardError = true;
                process.StartInfo.RedirectStandardInput = true;
                process.StartInfo.Arguments = argsLine;
                process.StartInfo.FileName = exePath;
    
                process.OutputDataReceived += (sender, eventArgs) =>
                {
                    Console.Write(eventArgs.Data);
                };
    
                process.ErrorDataReceived += (sender, eventArgs) =>
                {
                    Console.Error.Write(eventArgs.Data);
                };
    
                process.Exited += (sender, eventArgs) =>
                {
                    process.CancelOutputRead();
                    process.CancelErrorRead();
                    Environment.Exit(Environment.ExitCode);
                };
    
                process.Start();            
                process.BeginOutputReadLine();
                process.BeginErrorReadLine();                        
    
                while (true)
                {
                    Thread.Sleep(20);
                    string line = Console.ReadLine();
                    if (line != null)
                    {
                        process.StandardInput.WriteLine(line);
                    }
                }            
            }
        }
    }
    

提交回复
热议问题