将应用程序带到前台进行输入后,C#暂停 [英] C# Pause After Bringing Application to Foreground for Input

查看:81
本文介绍了将应用程序带到前台进行输入后,C#暂停的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个被调用为新线程的方法,如下所示:

I have a method that gets called into a new thread like so:

    if (!_isPlaying)
    {
        _playBackThread = new Thread(PlayMacroEvents);
        _playBackThread.Start();
        ...
    }

方法如下:

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)
                    {
            ...

我遇到的问题是,如果记事本尚未启动(未最小化),则在设置前景窗口和宏输出之间会有足够的延迟,从而通常不会显示第一批命令.如何在宏开始插入之前留出足够的时间来确保窗口已打开?在SetForegroundWindow()for循环之间的Thread.Sleep()似乎不起作用.想法?

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?

推荐答案

删除第一批输入的原因是因为我还必须像这样将ShowWindow的命令包括在内..

The reason the first series of inputs were being dropped is because I also had to include the command to ShowWindow like so..

在类标题中:

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);
    }

外观如下:

   if (proc != null)
    {

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

这篇关于将应用程序带到前台进行输入后,C#暂停的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆