The issue recreated in code:
class Program
{
private const int VK_ESCAPE = 0x1B;
private const int WM_KEYDOWN = 0x0100;
[DllImport("User32.dll")]
private static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
static void Main(string[] args)
{
Task.Run(() =>
{
var hWnd = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
bool hasSucceeded = PostMessage(hWnd, WM_KEYDOWN, VK_ESCAPE, 0);
return;
});
Console.WriteLine("PostMessage works as expected in Debug >> Start Debugging mode.");
if ((Console.ReadKey()).Key == ConsoleKey.Escape)
return;
}
}
I'm trying to cancel Console.ReadKey()
by calling PostMessage
from a worker thread. This works when I run the code under Debug >> Start Debugging mode.
However, when I run the code under Debug >> Start Without Debugging mode it hangs? Any responses/solutions are greatly appreciated.