确定进程何时完成初始化 [英] Determining when a process has finalized initialization

查看:84
本文介绍了确定进程何时完成初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个用于初始化AutoCAD实例的IronPython模块,并且需要在AutoCAD完成初始化,进入其消息循环并处于稳定状态之后,将1返回到名为的模块.未加载依赖项/任何内容)状态.我试过使用System.Diagnostics.Process.WaitForInputIdle()没有运气.

I'm building an IronPython module that initializes an instance of AutoCAD and I need to return 1 to the module calling it after AutoCAD has finished initializing, entered its message loop and is in a stable (not loading dependencies/anything) state. I've tried using System.Diagnostics.Process.WaitForInputIdle() with no luck.

这是我到目前为止所拥有的:

Here's what I have so far:

import System.Diagnostics as sysdiag

def start_autocad(self):
    print("\"C:\\Program Files\\Autodesk\\Autodesk AutoCAD Civil 3D 2014\\acad.exe\" /b \"C:\\Temp\\C3DAutoScript.scr\"")
    for process in sysdiag.Process.GetProcessesByName("acad"):
        process.Kill()
    try:
        acad_process = sysdiag.Process.Start("C:\\Program Files\\Autodesk\\Autodesk AutoCAD Civil 3D 2014\\acad.exe", " /b \"C:\\Temp\\C3DAutoScript.scr\"")
        acad_process.WaitForInputIdle()
        return 1
    except:
        return 0

不幸的是,此功能按原样会在进程开始打开后立即返回,而不是在完成后返回.有没有人知道在经典的cPython,IronPython或C#中解决此问题的方法(无需使用过分的睡眠函数来等待它)?

Unfortunately, this function as it stands returns as soon as the process begins opening, not after it's done. Does anyone know a way to handle this either in classic cPython, IronPython or C# (without using an overkill sleep function to wait for it)?

推荐答案

一个进程几乎可以在启动后立即开始处理消息.不需要在执行此操作之前先显示主窗口.这样做可以避免在加载缓慢的情况下显示为悬挂状态.

A process can start processing messages almost immediately after start up. There is no requirement that the main window is displayed before this is done. This can be done to avoid being displayed as hanging, in case loading is slow.

您可以尝试在WaitForInputIdle返回之后与该进程进行交互-即使加载"它最终也会响应.如果不起作用,请等待主窗口出现(使用FindWindow).如果应用程序是COM服务器,则尝试建立COM连接.

You can either try interacting with the process after WaitForInputIdle returns — it may eventually respond even while "loading". If it doesn't work, wait for the main window to appear (using FindWindow). If the application is COM server, you try establishing COM connection.

P.S.终止进程不是最好的主意,它可能会导致数据或配置损坏.尝试通过向其主窗口发送关闭事件来正确关闭应用程序.

P.S. Killing processes is not the best idea and may result in corrupted data or configuration. Try closing application properly be sending close event to its main window.

这篇关于确定进程何时完成初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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