使用Process.GetProcesses()然后MainWindowTitle过滤GUI应用程序的问题。 [英] Problem with Using Process.GetProcesses()then MainWindowTitle to filter GUI Applications.

查看:250
本文介绍了使用Process.GetProcesses()然后MainWindowTitle过滤GUI应用程序的问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在尝试使用GetProcesses构建Task Manger的Applications部分,然后使用MainWindowTitle来过滤GUI应用程序。这对我来说很有用,但我现在已经注意到使用MainWindowTitle并不总是有效,因为如果应用程序处于非活动状态(此非活动时间随机更改但特别是Outlook发生),则MainWindowTitle消失并变为空,因此它不会显示在我的应用程序列表中。然后我需要点击缺失的应用程序才能再次显示它。我想知道这是否正常,如果MainWindowTitle属性字段在不活动时变为空,然后再次出现?



我是在互联网上搜索这个属性是过滤GUI应用程序的最佳方法(另一种方法是使用EnumDesktopWindows)的假设,除非有其他人有遇到我可以用的其他东西?



提前谢谢你。 :)

Hi everyone,

I’m trying to build the "Applications" section of Task Manger using GetProcesses and then MainWindowTitle to filter GUI Applications. This worked well for me to begin with but I’ve now noticed that using "MainWindowTitle" does not always work because if an application has been inactive (This inactive time randomly changes but happens especially with "Outlook"), the "MainWindowTitle" disappears and becomes empty so it does not show on my Application list. I would then need to click on the "missing" application for it to be shown again. I was wondering if this sounds normal that the "MainWindowTitle" property field would become empty if inactive and then reappear again?

I was under the assumption from searching around the internet that this property is the best way to filter GUI applications (Another method would be to use EnumDesktopWindows) unless anyone else has come across something else I could use?

Thank you in advance. :)

推荐答案

这不是真的,它与激活窗口无关。当应用程序尚未准备好暴露窗口句柄时,会出现问题;它比应用程序运行时的早期阶段更明显,甚至没有创建窗口句柄;并且,在更罕见的情况下,某些应用程序可能会在运行时中间某处更改主窗口,因此在应用程序运行时可能会出现这种情况的可能性很小或很小。



请查看文档: http:/ /msdn.microsoft.com/en-us/library/system.diagnostics.process.mainwindowtitle.aspx [ ^ ]。



即使如果由于进程已退出而未定义MainWindowTitle属性,则会将InvalidOperationException 记录为抛出,此文档不太准确;在其他情况下可能会发生这种情况。



因此,不幸的是,您的代码应该预期异常并正确处理它。一个建议是尝试再次找到标题,但它只适用于某些过程,因为有些人从来没有这样的事情。



我应该注意到按标题名称过滤运行进程的整个想法非常糟糕。我的意思是,没有什么能保证完全不相关的进程具有相同的主窗口标题名称。不幸的是,流程名称的状态相同。首先,如果你真的需要这样的功能,我会认真思考。 (原则上,如果您解释为什么需要它,我们可以讨论它。)



-SA
This is not true, it is not related to activation of a window. The problem appears when the application is not yet ready to expose the window handle; it''s apparent than in early phases of application runtime a window handle is not even created; and, in more rare cases, some applications may change the main window somewhere in the middle of runtime, so there may or may not be a small probability of having this situation in the middle of application runtime.

Please take a look at documentation: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.mainwindowtitle.aspx[^].

Even though InvalidOperationException is documented as thrown if "MainWindowTitle property is not defined because the process has exited", this documentation is not quite accurate; it may happen in other cases.

So, unfortunately, your code should expect an exception and handle it properly. One suggestion is to try to find the title at later time again, but it will work only for some processes, as some never have such thing.

I should note that the whole idea of filtering running processes by title name is pretty bad. I mean, nothing can guarantee that totally unrelated processes have same main window title name. Unfortunately, the same states fro process names. First of all, I would think hard if you really need such functionality. (In principle, we can discuss it if you explain why would you need it.)

—SA

这篇关于使用Process.GetProcesses()然后MainWindowTitle过滤GUI应用程序的问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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