问题
I log my messages to the console. But I have some interesting effect. My UI hangs when I select some messages in the console. UI will alive when I deselect all in the console. I do not like such behavior.
Is it any solution?
Console::WriteLine("Message");
回答1:
You're selecting text in the console. You can imagine that if the console continued outputting information it would be hard to select what you want to select, so the system pauses visible updates. If you want your application to continuing showing visible updates, stop selecting text on the screen.
回答2:
You can disable quick edit mode in your console programmatically:
class Program {
static void Main(string[] args) {
uint mode;
IntPtr stdIn = GetStdHandle(STD_INPUT_HANDLE);
if (GetConsoleMode(stdIn, out mode)) {
if ((mode & (uint) ConsoleModes.ENABLE_QUICK_EDIT_MODE) != 0) {
mode = mode ^ (uint) ConsoleModes.ENABLE_QUICK_EDIT_MODE;
SetConsoleMode(stdIn, mode);
}
}
int i = 0;
while (true) {
Thread.Sleep(300);
Console.WriteLine(i++);
}
}
const int STD_INPUT_HANDLE = -10;
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr GetStdHandle(int nStdHandle);
[DllImport("kernel32.dll")]
static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode);
[DllImport("kernel32.dll")]
static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode);
[Flags()]
enum ConsoleModes : uint {
ENABLE_PROCESSED_INPUT = 0x1,
ENABLE_LINE_INPUT = 0x2,
ENABLE_ECHO_INPUT = 0x4,
ENABLE_WINDOW_INPUT = 0x8,
ENABLE_MOUSE_INPUT = 0x10,
ENABLE_INSERT_MODE = 0x20,
ENABLE_QUICK_EDIT_MODE = 0x40,
ENABLE_EXTENDED_FLAGS = 0x80,
ENABLE_AUTO_POSITION = 0x100,
}
}
回答3:
This is not specific to your application, it's "feature" exists for all console applications. When selecting text - the whole application hangs. This behavior can't be changed.
来源:https://stackoverflow.com/questions/15750276/my-ui-hangs-when-i-select-some-area-in-the-console