.NET System.Process:当您重定向StandardError时,控制台窗口未收到任何输出

时间:2018-07-10 04:35:48

标签: c# .net vb.net process io-redirection

我想获取控制台程序的错误输出,以防它崩溃。但是我希望标准输出显示在控制台窗口中。但是,如果我重定向标准错误,则控制台窗口将不会输出任何内容。

Dim p As New Process
p.StartInfo.Filename = filename
p.StartInfo.UseShellExecute = False
p.StartInfo.RedirectStandardError = True
p.EnableRaisingEvents = True
p.Start
p.BeginReadErrorLine

因此,现在如果启动的进程(控制台程序)崩溃,我将得到预期的错误输出。但是,标准输出在控制台程序中不可见。

我写了一个测试程序来启动。

    static void Main(string[] args)
    {

        for (int i = 0; i < 1000; i++)
        {
            Console.WriteLine(i);
        }

        object m = null;
        string s = m.ToString();

    }

我希望在控制台窗口中看到数字0-999滚动并通过重定向获得错误输出,但是除非没有任何重定向,否则在控制台窗口中什么也看不到。

我没有发现任何与此问题有关的问题。这是缺陷还是我忽略了的东西?

1 个答案:

答案 0 :(得分:0)

我尝试过复制,但是我不能。这是我仅重定向错误的代码,但仍然可以在控制台窗口中看到输出:

Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Users\User\AppData\Local\Programs\Python\Python36- 
32\lib\multiprocessing\spawn.py", line 105, in spawn_main
exitcode = _main(fd)
File "C:\Users\User\AppData\Local\Programs\Python\Python36- 
32\lib\multiprocessing\spawn.py", line 114, in _main
prepare(preparation_data)
File "C:\Users\User\AppData\Local\Programs\Python\Python36- 
32\lib\multiprocessing\spawn.py", line 225, in prepare
_fixup_main_from_path(data['init_main_from_path'])
File "C:\Users\User\AppData\Local\Programs\Python\Python36- 
32\lib\multiprocessing\spawn.py", line 277, in _fixup_main_from_path
run_name="__mp_main__")
File "C:\Users\User\AppData\Local\Programs\Python\Python36-32\lib\runpy.py", 
line 263, in run_path
pkg_name=pkg_name, script_name=fname)
File "C:\Users\User\AppData\Local\Programs\Python\Python36-32\lib\runpy.py", 
line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
File "C:\Users\User\AppData\Local\Programs\Python\Python36-32\lib\runpy.py", 
line 85, in _run_code
exec(code, run_globals)
File "C:\Users\User\PycharmProjects\sampledemo\datafeed.py", line 24, in 
<module>
pool = Pool(nprocs)
File "C:\Users\User\AppData\Local\Programs\Python\Python36- 
32\lib\multiprocessing\context.py", line 119, in Pool
context=self.get_context())
File "C:\Users\User\AppData\Local\Programs\Python\Python36- 
32\lib\multiprocessing\pool.py", line 174, in __init__
self._repopulate_pool()
File "C:\Users\User\AppData\Local\Programs\Python\Python36- 
32\lib\multiprocessing\pool.py", line 239, in _repopulate_pool
w.start()
File "C:\Users\User\AppData\Local\Programs\Python\Python36- 
32\lib\multiprocessing\process.py", line 105, in start
self._popen = self._Popen(self)
File "C:\Users\User\AppData\Local\Programs\Python\Python36- 
32\lib\multiprocessing\context.py", line 322, in _Popen
return Popen(process_obj)
File "C:\Users\User\AppData\Local\Programs\Python\Python36- 
32\lib\multiprocessing\popen_spawn_win32.py", line 33, in __init__
prep_data = spawn.get_preparation_data(process_obj._name)
File "C:\Users\User\AppData\Local\Programs\Python\Python36- 
32\lib\multiprocessing\spawn.py", line 143, in get_preparation_data
_check_not_importing_main()
File "C:\Users\User\AppData\Local\Programs\Python\Python36- 
32\lib\multiprocessing\spawn.py", line 136, in _check_not_importing_main
is not going to be frozen to produce an executable.''')
RuntimeError: 
    An attempt has been made to start a new process before the
    current process has finished its bootstrapping phase.

    This probably means that you are not using fork to start your
    child processes and you have forgotten to use the proper idiom
    in the main module:

        if __name__ == '__main__':
            freeze_support()
            ...

    The "freeze_support()" line can be omitted if the program
    is not going to be frozen to produce an executable.

这是我的输出:

enter image description here

我是按照您尝试的方式做其他事情吗?