- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试通过单击按钮从 winform 应用程序打开控制台。我正在通过以下代码执行此操作。
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool AllocConsole();
[DllImport("kernel32.dll")]
static extern Boolean FreeConsole();
private void button1_Click(object sender, EventArgs e)
{
int userInput = 0;
AllocConsole();
do
{
Console.Clear();
Console.WriteLine("Hello World");
Console.WriteLine("Select 1 or 2");
int.TryParse(Console.ReadLine(), out userInput);
} while (userInput != 1 && userInput != 2);
FreeConsole();
}
第一次打开控制台时,它工作正常。尝试再次打开控制台可以正常工作,但一次 Console.Clear();
被调用,我得到:
An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll
Additional information: The handle is invalid.
相同的异常将在 Console.WriteLine();
上抛出和 Console.ReadLine();
.我已经尝试了 this 中提出的解决方案, this , 和 this ,但我最终得到了相同的“句柄无效”。 Console.Clear();
上的错误.
我试过的一些额外代码:
[DllImport("kernel32.dll",
EntryPoint = "GetStdHandle",
SetLastError = true,
CharSet = CharSet.Auto,
CallingConvention = CallingConvention.StdCall)]
private static extern IntPtr GetStdHandle(int nStdHandle);
[DllImport("kernel32.dll",
EntryPoint = "AllocConsole",
SetLastError = true,
CharSet = CharSet.Auto,
CallingConvention = CallingConvention.StdCall)]
private static extern int AllocConsole();
[DllImport("kernel32.dll")]
static extern Boolean FreeConsole();
private const int STD_OUTPUT_HANDLE = -11;
private const int MY_CODE_PAGE = 437;
private void button1_Click(object sender, EventArgs e)
{
int userInput = 0;
AllocConsole();
IntPtr stdHandle = GetStdHandle(STD_OUTPUT_HANDLE);
SafeFileHandle safeFileHandle = new SafeFileHandle(stdHandle, true);
FileStream fileStream = new FileStream(safeFileHandle, FileAccess.Write);
Encoding encoding = System.Text.Encoding.GetEncoding(MY_CODE_PAGE);
StreamWriter standardOutput = new StreamWriter(fileStream, encoding);
standardOutput.AutoFlush = true;
Console.SetOut(standardOutput);
Stream streamIn = Console.OpenStandardInput();
TextReader readerIn = new StreamReader(streamIn);
Console.SetIn(readerIn);
do
{
Console.Clear();
Console.WriteLine("Hello World");
Console.WriteLine("Select 1 or 2");
int.TryParse(Console.ReadLine(), out userInput);
} while (userInput != 1 && userInput != 2);
FreeConsole();
}
这对于 Console.WriteLine();
似乎没问题和 Console.ReadLine();
但我仍然无法解决 Console.Clear();
抛出的异常.谁能告诉我是否遗漏了什么?
最佳答案
我怀疑是否可以多次附加/分离到控制台。据我了解the code of private static IntPtr ConsoleInputHandle
(同样适用于 ConsoleOutputHandle
),句柄在第一次使用时初始化一次,因此当您从控制台分离进程并再次重新附加它时,句柄无效。
所以恕我直言,根据需要使用 Console
类是不可行的(我也不知道任何同时作为控制台和 Win32 应用程序工作的程序的实例 - 我见过的大多数应用程序都提供了两个.exe 版本)。
如果您真的需要 Windows 控制台,我想您可以尝试在 Windows API 之上提供您自己的控制台包装器。如果您只需要控制台的外观,您可以渲染您自己的“类似控制台”的窗口。
关于c# - 第二次打开控制台后尝试调用 Console.Clear() 时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47553684/
谁能告诉我为什么 的技术原因在 app.config 与 中无效 通常这样使用: 问题是我正在使用安装程序产品 (InstallShield) 将 xml 转换为 app.config
在Linux上,使用崇高的文本。Os.system(‘Clear’)应该只清除控制台窗口。用于Windows的os.system(‘cls’)。但它回来的时候和我不一样。。代码:。退货:。为什么在Cl
这个问题在这里已经有了答案: How can I force the STL memory cache to clear? (2 个答案) 关闭 6 年前。 我在 Solaris 10 上使用 g+
official docs关于 .clear() 我不是很清楚。他们说: Erases all AsyncStorage for all clients, libraries, etc. You pr
我有这段代码可以清除 C# WebBrowser 控件中的缓存。它的问题是它还会清除 cookie。在整个互联网上,我似乎是唯一一个不想这样的人。 我需要维护 cookie,但要丢弃缓存。 特别感兴趣
我记得直接在 DataTable 上调用的一些方法/属性之间存在差异。类,以及 DataTable.Rows 上同名的方法/属性属性(property)。 (可能是我读到这篇文章的 RowCount/
在 Unity 的 Camera组件中有一个属性清除标志,它允许从四个选项中进行选择:天空盒、纯色、仅深度和不清除。 正如文档所说: Don’t clear This mode does not cl
我无法找出为什么我的函数没有被调用。我将 alert("test") 放在那里只是为了测试函数是否被调用,而事实并非如此。 HTML: JS: function clear(price,quanti
我有 2 个数组列表: ArrayList> res= new ArrayList(); ArrayList data= new ArrayList(); 在我将结果集添加到子项并将子项附加到父项后,
正如主题所述..哪个版本更有效,为什么? std::vector a; .. a.clear(); 或 std::vector a; .. if(!a.empty()) a.clear(); 最佳
我包含了定义函数“clear()”的“PDCurses/curses.h”,然后当我使用“std::wstring::clear()”时,msvc-10.0 编译器报告错误。当我在包含后使用“#und
Session.Clear() 与 Session.Contents.Clear() 有什么区别? 我想清除所有 session 变量。 谢谢。 最佳答案 根据反射器,没有差异。 Session.Co
QPointer有一个方法, clear() . Clears this QPointer object. 我不确定“清晰”的确切含义。在我看来,这可能意味着 它会删除您引用的指针。 或 它取消附加您
在 string::clear 函数的描述中,它说: clear: Erases the contents of the string, which becomes an empty string (
我有两个不同的 Mathematica 笔记本,它们具有相似但功能不同的功能。当它们是唯一打开的笔记本时,两者都可以正常工作。尽管我(自由地)使用 Clear[] 来清除相关变量,但其中一个在另一个笔
有没有办法扩展 Symfony 2 cache:clear命令来清除 APC 还是执行一些其他逻辑? 最佳答案 您可以使用 ApcBundle去做这个。 关于php - 扩展 Symfony 2 缓存
我正在尝试使用 html 和 css 制作一个简单的信息网站,并使用 960 网格,因为该网站将具有分栏结构。 当我将标题 h1 float 到左侧(它有 Logo 图像,我正在使用 css 添加图像
我刚刚意识到: 在一系列 float div 导致布局破坏之后,同时 工作正常。 谁能解释一下? 这是 CSS: div.clear { clear:both; } 最佳答案 如果您将
这两个命令等效吗?如果不是,有什么区别? 最佳答案 rake 任务仅清除存储在文件系统 "#{Rails.root}/tmp/cache" 中的文件。这是该任务的代码。 namespace :cach
在STM32F407上初始化USART1时,我在启用TC中断时遇到了问题。一旦 USART RCC 启用,SR 中的 Tcflags就会被设置(“1”),而在启用 TC 中断之前清除该标志对我来说已经
我是一名优秀的程序员,十分优秀!