简体   繁体   English

将应用程序带到前台进行输入后,C#暂停

[英]C# Pause After Bringing Application to Foreground for Input

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. SetForegroundWindow()for循环之间的Thread.Sleep()似乎不起作用。 Ideas? 有想法吗?

使用一些api获取活动窗口,并等待直到属于记事本的窗口成为活动窗口

The reason the first series of inputs were being dropped is because I also had to include the command to ShowWindow like so.. 之所以删除第一批输入,是因为我还必须像这样向ShowWindow包含命令。

in the class header: 在类标题中:

private const int SW_RESTORE = 9;
[DllImport("user32")]
private static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
...

In the macro thread method I changed 在宏线程方法中,我更改了

    if (proc != null)
    {
        SetForegroundWindow(proc.MainWindowHandle);
    }

to look like: 看起来像:

   if (proc != null)
    {

        ShowWindow(proc.MainWindowHandle, SW_RESTORE);
        SetForegroundWindow(proc.MainWindowHandle);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM