Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
winform(C#)和python之间的命名管道_C#_Python_.net_Ipc_Named Pipes - Fatal编程技术网

winform(C#)和python之间的命名管道

winform(C#)和python之间的命名管道,c#,python,.net,ipc,named-pipes,C#,Python,.net,Ipc,Named Pipes,我正在开发一个窗口表单应用程序(C#),它应该启动一个外部进程,并在表单应用程序中显示来自exe的一些结果和/或错误消息。 我似乎无法在表单应用程序和用python制作的外部进程(exe)之间形成命名管道连接。exe启动并正常工作,但它似乎无法保存命名的管道连接。因此,我无法从exe获取任何消息。 exe是用pyinstaller制作的,命名管道连接在与窗口控制台应用程序配对时似乎工作得很好,即我可以将消息从exe返回到控制台应用程序 控制台应用程序 下面的脚本可以从控制台上的exe获取返回消息

我正在开发一个窗口表单应用程序(C#),它应该启动一个外部进程,并在表单应用程序中显示来自exe的一些结果和/或错误消息。 我似乎无法在表单应用程序和用python制作的外部进程(exe)之间形成命名管道连接。exe启动并正常工作,但它似乎无法保存命名的管道连接。因此,我无法从exe获取任何消息。
exe是用pyinstaller制作的,命名管道连接在与窗口控制台应用程序配对时似乎工作得很好,即我可以将消息从exe返回到控制台应用程序

控制台应用程序

下面的脚本可以从控制台上的exe获取返回消息


namespace pipeConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            NamedPipeClientStream pipeClient = new NamedPipeClientStream("teropipe");
            Console.Write("Attempting to connect to the pipe...");
            System.Diagnostics.Process.Start(@"my\path\to\external\app\tempp.exe");
            pipeClient.Connect();
            Console.WriteLine("Connected to the pipe");
            Console.WriteLine("There are currently {0} pipe server instances open.", pipeClient.NumberOfServerInstances);
            StreamWriter writer = new StreamWriter(pipeClient);
            StreamReader reader = new StreamReader(pipeClient);
            string temp;
            while ((temp = reader.ReadLine()) != null)
            {
                Console.WriteLine("Received from server: {0}", temp);
            }
            Console.Write("Press Enter to continue..");
            Console.ReadLine();
        }
    }

但当我在windowformapp上尝试类似的方法时,我无法从python服务器上获得任何信息

表单应用程序

虽然我在表单应用程序中使用了类似的方法,但是没有返回任何消息。事实上,命名管道连接似乎没有保持打开状态以便表单通信


namespace pipeDemoForm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void okayButton_Click(object sender, EventArgs e)
        {
            NamedPipeClientStream pipeClient = new NamedPipeClientStream("teropipe");
            //MessageBox.Show("attempting to connect");
            System.Diagnostics.Process.Start(@"my\path\to\external\app\tempp.exe");
            pipeClient.Connect();
            string numb = pipeClient.NumberOfServerInstances.ToString();
            MessageBox.Show("There are currently {0} pipe server instances open", numb);
            if (pipeClient.IsConnected)
            {
                MessageBox.Show("There are currently {0} pipe server instances open", pipeClient.NumberOfServerInstances.ToString());
            }
        }
    }
}
Python脚本-tempp.exe

以下是python脚本,我已使用pyinstaller将其打包到onefile exe中


import win32pipe
import win32file

def process():
    pipe_handle = win32pipe.CreateNamedPipe(r'\\.\pipe\teropipe',
                              win32pipe.PIPE_ACCESS_DUPLEX,
                              win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_WAIT,
                  1,65536,65536,300,None)
    win32pipe.ConnectNamedPipe(pipe_handle, None)
   # business logic
    ..................
    #write some output to the form app
   win32file.WriteFile(pipe_handle,"some string return from above")


您始终可以连接到流程的
标准输出