如何在没有进程立即退出的情况下在进程上重定向 StandardInput? [英] How redirect StandardInput on Process without process immediately exiting?

查看:37
本文介绍了如何在没有进程立即退出的情况下在进程上重定向 StandardInput?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个用于锁定文件的实用程序 lockfile.exe,它接受两个参数:fileName(要锁定的文件)和 fileShare(要应用的份额).然后我从另一个应用程序中调用这个实用程序来测试处理锁定文件时的错误情况.lockfile.exe 实用程序锁定具有指定共享的文件,然后等待任何字符输入以释放锁定并退出.

I created a utility to lock files lockfile.exe, which accepts two parameters: fileName (file to be locked) and fileShare (share to be applied). I then call this utility from within another application to test error conditions when processing locked files. The lockfile.exe utility locks a file with the specified share and then waits for any character input to release the lock and exit.

在我的测试应用程序中,我用两个方法 LockFileUnlockFile 创建了一个类 FileLocker 如下:

In my test application I created a class FileLocker with two methods LockFile and UnlockFile as follows:

public class FileLocker
{
    private Process process;

    public void LockFile(string fileName)
    {
        this.process = new Process();
        this.process.StartInfo.RedirectStandardInput = true;
        this.process.StartInfo.UseShellExecute = false;
        this.process.StartInfo.CreateNoWindow = true;
        this.process.StartInfo.FileName = "lockfile.exe";
        this.process.StartInfo.Arguments =
            "\"" + fileName + "\" " + FileShare.Read.ToString();
        this.process.Start();
    }

    public void UnlockFile()
    {
        this.process.StandardInput.Write('X');
        this.process.StandardInput.Flush();
        this.process.WaitForExit();
        this.process.Close();
    }
}

当我调用方法 LockFile 时,进程在启动后立即退出.它不会等待 UnlockFile 中的输入.当我不重定向标准输入时,进程会等待键盘输入.

When I call the method LockFile the process immediately exits after starting. It does not wait for the input in UnlockFile. When I do not redirect the standard input, then the process waits for keyboard input.

我需要更改/修复什么,以便进程不会立即退出.我需要它等待 UnlockFile 中提供的输入,然后进程才应该退出?

What do I need to change/fix, so that the process does not immediately exit. I need it to wait for the input provided in UnlockFile and only then should the process exit?

更新:

更新以显示上面的 FileLocker 类并提供下面的示例调用:

Updated to show FileLocker class above and to provide sample calls below:

class Program
{
    static void Main(string[] args)
    {
        FileLocker locker = new FileLocker();
        locker.LockFile("HelloWorld.txt");
        locker.UnlockFile();
    }
}

推荐答案

问题很可能是您在 lockfile.exe 应用程序中使用了 Console.ReadKey().这不接受来自标准输入的输入,而是使用低级键盘挂钩来检查您无法从其他应用程序发送的按键.

The problem is likely that you are using Console.ReadKey() in your lockfile.exe application. This does not accept input from standard input but uses low-level keyboard hooks to check for key presses which you cannot send from another application.

我用 LockFile.exe 测试了你的程序,它只包含一个简单的 Console.ReadLine(),如果我改变 Write('X') 调用 WriteLine("X") (因为调用 readline 在返回之前需要换行).所以我怀疑你的问题出在 lockfile.exe 程序而不是这个程序中.确保您使用 Console.ReadLine() 等待来自 lockfile.exe 应用程序中标准输入的输入.

I tested your program with a LockFile.exe that just contained a simple Console.ReadLine(), and it works just as expected if I change the Write('X') call to a WriteLine("X") (since the call to readline needs a newline before returning). So I'm suspecting your problem is in the lockfile.exe program rather than this one. Ensure you are using Console.ReadLine() to wait for input from standard input in your lockfile.exe application.

这篇关于如何在没有进程立即退出的情况下在进程上重定向 StandardInput?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆