SendKeys in c# - not able to send multiple keys

1.4k Views Asked by At

I am actually activating the button click event of the VB application through my c# code - I have created shortcut key of the button click and calling with the help of below code.

The code is working fine but button 2 click event is not firing.

1.Step - to call the Click event of button1 of VB project

  ProcessStartInfo startInfo = new ProcessStartInfo();
  startInfo.FileName = @"C:\New\MyProj.exe";
  Process process = Process.Start(startInfo);
  IntPtr h = process.MainWindowHandle;
  SetForegroundWindow(h);
  // Send it Alt-X
  SendKeys.SendWait("%x");
  Thread.Sleep(30000);

2.Step - to call one bat file

  Process process1 = null;
  ProcessStartInfo startInfo1 = new ProcessStartInfo();
  startInfo1.FileName = @"C:\App\Test.bat";
  process1 = Process.Start(startInfo1);
  Thread.Sleep(10000);

3.Step - to call the Click event of button2 of VB project

   //again i am setting main window as my VB project and calling another click event.
   SetForegroundWindow(h);
   SendKeys.SendWait("%h");
   Thread.Sleep(10000);

The last click event is not working, why? - Alt +h is not firing

1

There are 1 best solutions below

1
On BEST ANSWER

Try to kill the first process by using process.kill() and again start the VB project by

   startInfo = new ProcessStartInfo();
   startInfo.FileName = @"C:\New\MyProj.exe";
   Process process = Process.Start(startInfo);
   process.WaitForInputIdle();
   SetForegroundWindow(h);
   SendKeys.SendWait("%h");