I have a method that gets called into a new thread like so:
if (!_isPlaying)
{
_playBackThread = new Thread(PlayMacroEvents);
_playBackThread.Start();
...
}
The method looks like:
Process proc = Process.GetProcessesByName("notepad").FirstOrDefault();
if (proc != null)
{
SetForegroundWindow(proc.MainWindowHandle);
}
int loopCount = this.dsUserInput.Tables[0].Rows.Count;
for (int i = 0; i < loopCount; i++)
{
foreach(MacroEvent macroEvent in _events)
{
Thread.Sleep(macroEvent.TimeSinceLastEvent);
switch (macroEvent.MacroEventType)
{
...
The problem I'm having is that if notepad is not already up (not minimized) there is enough delay between setting the foreground window and the macro output that often the first series of commands is not shown. How can I put enough of a pause to make sure that the window is up before the the macros start kicking in? A Thread.Sleep()
between SetForegroundWindow()
and the for
loop does not seem to do the trick. Ideas?