Keeping focus on console application

206 Views Asked by At

I'm creating a program where I control excel through the console application, but I need constant focus on the console. I already tried some methods like the SetForegroundWindow() but it didn't work. How can I do this? I need it to be inside the while loop so that, everytime I insert a character (code that is done so i won't have to press enter everytime) it stays in the console application and doesn't change focus do excel.

foreach (Process p in prcss)
        {
            p.WaitForInputIdle();
            IntPtr h = p.MainWindowHandle;
            SetForegroundWindow(h);

            if (p.ProcessName == prog || p.ProcessName == progcaps)
            {
                Console.WriteLine("\n\nProcess: {0} ID: {1}\n", p.ProcessName, p.Id); //Imprime apenas o que queremos        

            }

            string pname = p.ProcessName;

            //Enter
            SendKeys.SendWait("~");
            SendKeys.SendWait("{F2}");

            while (true)
            {
                //Code goes here
            }
       }
0

There are 0 best solutions below