AIR NativeProcess standardInput not connecting to app

后端 未结 2 1716
名媛妹妹
名媛妹妹 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:45

    I encountered this issue a few months back but I never resolved it as I just used command line args instead. I have just returned to it though as I am keen to find out know what's going on.

    I have now found that targeting .NET 3.5 or earlier makes it work as expected for me. Switch back to to v4.0 and I get nothing on stdin. I'm not exactly sure where the issue lies, but if you can get by with v3.5 then it might be a solution for you.

    0 讨论(0)
  • 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<string>(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);
                    }
                }            
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题