[quote=“cadaver”]I recommend using the WorkQueue & tasks only for short tasks that need to repeat from frame to frame and benefit from multicore scaling. Something like reading console input sounds like a job for the Thread class, which represents a long-lived thread and is fairly simple to use. Search “public Thread” from Urho code to see where it’s being subclassed & used.
However, for this exact task the ProcessUtils.h also includes a non-blocking console read function, GetConsoleInput(), which you should be able to use in the main thread, so if it works well for you you shouldn’t need to even create a thread.[/quote]
Yea. I found the code.
[code]String GetConsoleInput()
{
String ret;
#ifdef URHO3D_TESTING
// When we are running automated tests, reading the console may block. Just return empty in that case
return ret;
#endif
#ifdef WIN32
HANDLE input = GetStdHandle(STD_INPUT_HANDLE);
HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE);
if (input == INVALID_HANDLE_VALUE || output == INVALID_HANDLE_VALUE)
return ret;
// Use char-based input
SetConsoleMode(input, ENABLE_PROCESSED_INPUT);
INPUT_RECORD record;
DWORD events = 0;
DWORD readEvents = 0;
if (!GetNumberOfConsoleInputEvents(input, &events))
return ret;
while (events--)
{
ReadConsoleInputW(input, &record, 1, &readEvents);
if (record.EventType == KEY_EVENT && record.Event.KeyEvent.bKeyDown)
{
unsigned c = record.Event.KeyEvent.uChar.UnicodeChar;
if (c)
{
if (c == '\b')
{
PrintUnicode("\b \b");
int length = currentLine.LengthUTF8();
if (length)
currentLine = currentLine.SubstringUTF8(0, length - 1);
}
else if (c == '\r')
{
PrintUnicode("\n");
ret = currentLine;
currentLine.Clear();
return ret;
}
else
{
// We have disabled echo, so echo manually
wchar_t out = c;
DWORD charsWritten;
WriteConsoleW(output, &out, 1, &charsWritten, 0);
currentLine.AppendUTF8(c);
}
}
}
}
#elif !defined(ANDROID) && !defined(IOS)
int flags = fcntl(STDIN_FILENO, F_GETFL);
fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
for (;;)
{
int ch = fgetc(stdin);
if (ch >= 0 && ch != '\n')
ret += (char)ch;
else
break;
}
#endif
return ret;
}[/code]