Click here to Skip to main content
15,915,869 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear all,


I've a problem about to send key immediately after launch the execute file in batch file, if shell from execute file directly is not a problem,
please see as below in my code.

'This is worked (run from directly execute file)
A.
VB
Dim ProcID AS Integer
Shell("notepad.exe",AppWinStyle.NormalFocus) ' this line is worked
AppActivate(ProcID)
SendKeys.SendWait("Hello world")

'This is not worked (run from batch file)
B.
VB
Dim ProcID AS Integer
Process.start("c:\gm.bat",AppWinStyle.NormalFocus) 'this line is not worked
AppActivate(ProcID)
SendKeys.SendWait("Hello world")


----- in c:\gm.bat -------
call c:\windows\system32\notepad.exe


please help me.

Best Regards,

[edit]Code block added[/edit]
Posted
Updated 2-Mar-14 23:11pm
v3

When you launch an external process using the Process class, call WaitForInputIdle on the Process object you created. You're not giving any time for the process to actually start before you send keys to it.
 
Share this answer
 
Comments
Member 3802896 4-Mar-14 0:46am    
I've tried this solution, but it still not worked.

Thanks a lot.
ok, a little bit tricky. the main thread of batch execution enters wait state after spawning notepad.exe. therefore, you can poll its state.

C#
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Test_ExecuteFromBatch
{
    class Program
    {
        static void Main(string[] args)
        {

            try
            {

                int bid;
                // run batch file
                using (Process p = Process.Start("npad.bat"))
                {
                    bid = p.Id;
                    // wait its main thread state to be 'wait'
                    while (p.Threads[0].ThreadState != System.Diagnostics.ThreadState.Wait)
                    {
                        Thread.Sleep(500);
                        Console.WriteLine(p.Threads[0].ThreadState);
                        p.Refresh();
                    }
                };
                // list all 'notepad's
                var l = Process.GetProcessesByName("notepad").Select(i1 =>
                new
                {
                    ProcessName = i1.ProcessName,
                    Id = i1.Id,
                    StartTime = i1.StartTime
                }).OrderByDescending(i1 => i1.StartTime);

                int id = -1;
                foreach (var item in l)
                {
                    Console.WriteLine("{0} {1} {2}", item.ProcessName, item.Id, item.StartTime);
                    // check if batch is parent of this notepad
                    if (id < 0 && bid == GetOwnerPid(item.Id))
                        id = item.Id;
                }
                if (id > 0)
                {
                    // Do what you want.
                    Console.WriteLine("{0}/{1}", bid, id);
                    Console.WriteLine(Process.GetProcessById(id).MainWindowTitle);
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            Console.ReadKey();
        }

        // wmi query for parent
        static int GetOwnerPid(int pid)
        {
            int oid = -1;
            string q = string.Format("Select * from Win32_Process Where ProcessID = {0}", pid);
            using (ManagementObjectSearcher s = new ManagementObjectSearcher(q))
            {
                var o = s.Get().Cast<ManagementObject>().FirstOrDefault();
                if (o != null)
                {
                    oid = Convert.ToInt32(o.GetPropertyValue("ParentProcessId"));
                }
            }
            return oid > 0 ? oid : -1;
        }
    }
}
 
Share this answer
 
Comments
Member 3802896 4-Mar-14 0:46am    
I've tried this solution, but it still not worked.

Thanks a lot.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900