问题描述
我有一个控制台应用程序,无需用户干预即可执行我的代码。如果用户有意或无意中在控制台窗口中单击,所有执行将停止。
I've got a console application that executes my code without user interaction. If the user clicks within the console window, on purpose or on accident, all execution stops.
这与从控制台窗口复制文本有关。应用程序再次开始执行的唯一方法是用户选择文本,然后在控制台窗口上单击鼠标右键,然后将其复制到剪贴板。
This has something to do with copying text from the console window. The only way for the application to start executing again is if the user selects text and then right-clicks on the console window, copying it to the clipboard.
实际上,创建一个控制台应用程序并添加以下代码。
To see this in action, create a console application and add the following code.
class Program
{
static void Main(string[] args)
{
var task = Task.Run(async () =>
{
int i = 0;
while (true)
{
Console.WriteLine(i++);
await Task.Delay(1000);
}
});
Console.ReadLine();
}
}
在控制台窗口上单击时,任务线程停止执行。这根本不是理想的行为,我想防止这种情况在我的控制台应用程序中发生。
When you click on the console window, the Task thread stops executing. This is not desirable behavior at all, and I want to prevent this from happening in my console application.
如何防止这种情况发生?据我所知,控制台窗口上的属性/事件均与控制此行为无关。
How can I prevent this? None of the properties/events on the console window have anything to do with controlling this behavior, as far as I can see.
正如我所见,当我在窗口中单击会出现光标。当我按任意键-光标消失并且应用程序继续运行时,
As you can see, when i'm click within window appear cursor. When i press any key - cursor gone and app continue working
推荐答案
如果在控制台窗口上启用了快速编辑模式,则会发生这种情况。如果右键单击标题栏并选择属性,然后选择选项选项卡,则可以检查是否启用了快速编辑模式。如果禁用了快速编辑模式,则在窗口中单击时滚动不会停止。
This happens if you have Quick Edit Mode enabled on the console window. If you right-click on the title bar and select Properties, then select the Options tab, you can check to see if Quick Edit Mode is enabled. If you disable Quick Edit Mode, then the scrolling doesn't stop when you click in the window.
滚动停止的原因是因为使用了鼠标在窗口中单击来选择文本。
The reason scrolling stops is because a mouse clicked in the window is used to select text.
您可以在程序的控制台上禁用快速编辑模式,但是需要调用和 API函数。这是您的操作方式:
You can disable Quick Edit Mode on the console in your program, but doing so requires calling the GetConsoleMode and SetConsoleMode API functions. Here's how you would do it:
[DllImport("kernel32.dll", SetLastError=true)]
public static extern IntPtr GetConsoleWindow();
[DllImport("kernel32.dll", SetLastError=true)]
public static extern bool GetConsoleMode(
IntPtr hConsoleHandle,
out int lpMode);
[DllImport("kernel32.dll", SetLastError=true)]
public static extern bool SetConsoleMode(
IntPtr hConsoleHandle,
int ioMode);
/// <summary>
/// This flag enables the user to use the mouse to select and edit text. To enable
/// this option, you must also set the ExtendedFlags flag.
/// </summary>
const int QuickEditMode = 64;
// ExtendedFlags must be combined with
// InsertMode and QuickEditMode when setting
/// <summary>
/// ExtendedFlags must be enabled in order to enable InsertMode or QuickEditMode.
/// </summary>
const int ExtendedFlags = 128;
void DisableQuickEdit()
{
IntPtr conHandle = GetConsoleWindow();
int mode;
if (!GetConsoleMode(conHandle, out mode))
{
// error getting the console mode. Exit.
return;
}
mode = mode & ~(QuickEditMode | ExtendedFlags);
if (!SetConsoleMode(conHandle, mode))
{
// error setting console mode.
}
}
void EnableQuickEdit()
{
IntPtr conHandle = GetConsoleWindow();
int mode;
if (!GetConsoleMode(conHandle, out mode))
{
// error getting the console mode. Exit.
return;
}
mode = mode | (QuickEditMode | ExtendedFlags);
if (!SetConsoleMode(conHandle, mode))
{
// error setting console mode.
}
}
如果沿着这条路线走,那可能是个不错的选择想法是在程序启动时保存原始控制台模式设置,并在程序退出时恢复原始设置。因此,在启动时:
If you go down this route, it's probably a good idea to save the original console mode setting when your program starts, and restore it when your program exits. So at startup:
GetConsoleMode(GetConsoleWindow(), ref saveConsoleMode);
以及程序终止时:
SetConsoleMode(GetConsoleWindow(), saveConsoleMode);
当然,要进行适当的错误处理。如果对 GetConsoleMode
的调用失败,您将不想恢复控制台模式。
With appropriate error handling, of course. You wouldn't want to restore the console mode if the call to GetConsoleMode
failed.
这篇关于当用户单击控制台窗口时,代码停止执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!