I'm running a window 2012 server instance on aws.
Windows_Server-2012-R2_RTM-English-64Bit-Base-2018.05.09 (ami-3c0f22d7)
Instance Id - i-0cd5e4853062d3e69
On the instance I'm running a script that does some automation (c#), mouse clicks/ keyboard clicks etc.
The script works fine till I decide to minimize/close the window of the instance. Once I drop the focus from the instance I get an error on the server:
System.ComponentModel.Win32Exception: Access is denied
Can't see the logic behind it, as it should keep working the same way regardless if I focus or even currently logged to the remote desktop isn't it?
Any suggestions?
Code sample -
static public void StayAlive()
{
IntPtr hWnd = FindFocusWindow();
Stopwatch s = new Stopwatch();
s.Start();
while (s.Elapsed < TimeSpan.FromSeconds(1))
{
SendKeys.SendWait("{LEFT}");
}
s.Stop();
s = new Stopwatch();
s.Start();
while (s.Elapsed < TimeSpan.FromSeconds(1))
{
SendKeys.SendWait("{RIGHT}");
}
s.Stop();
}
static private IntPtr FindFocusWindow()
{
IntPtr hWnd = IntPtr.Zero;
//Look for chrome and set to top
foreach (Process pList in Process.GetProcesses())
{
if (pList.MainWindowTitle.Contains("Google Chrome"))
{
hWnd = pList.MainWindowHandle;
ShowWindow(hWnd, 3);
SetForegroundWindow(hWnd); //set to topmost
return hWnd;
}
}
return hWnd;
}
Thanks, Ben.
So... after digging into this, couldn't find a solution that worked using code.
Using the method offered in the link below has solved my issue -
https://stackoverflow.com/a/36190639/7210967
Cheers, Ben.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.