Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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. :)
Posted

1 solution

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
 
Share this answer
 
Comments
Member 8288668 16-Jul-13 12:56pm    
I'm really sorry it has taken this long to get back to you but I didn't know I had a response till now! I really appreciated your answer and I agree that the method I've used is bad.

Basically I have multiple users on the same server and I want to give them the ability to force close applications when they get stuck but I also don't want them to close each others programs or see other unnecessary things you get in Task Manager.

Ideally, as a side project I'm trying to build Task Manager with just the Applications Tab.

I think EnumDesktopWindows is the way forward because from what I've seen on the Internet, it's how Task Manager does it but I'm open to suggestions.

Thanks
Sergey Alexandrovich Kryukov 16-Jul-13 13:20pm    
Well, first of all, no problem with long time at all. If you have problem with getting notifications on the post related to your posts, review your settings: click here.

Now, I cannot agree that you need to create another feature similar to Task Manager. I think if the problem is that your users are stuck with some applications, you should address the problem of getting stuck, not killing the processes. If you are talking about the processes that are not yours, killing them is not your business. There are Task Manager and, for example, Sysinternals Process Explorer, and probably more. It's the business of your users to use those utilities. And, finally, if some of your applications use external processes (I don't know if you do it or not), this would be the worse. Just don't do such things, try to make your application a single process. Processes are isolated, not designed to be used instead of a library or services.

And if you, by whatever reasons, really need to build the system killing processes, EnumerateDesktopWindows won't help you at all, apparently because a process doesn't have to be shown on a desktop, and because the same process can have more than one top-level windows. Better enumerate processes. My advice will help you to resolve the problem, so I think you can accept the answer formally (green button).

—SA
Member 8288668 17-Jul-13 5:36am    
Thanks SA for responding.

I understand the reasons you mentioned but I think we're not on the right page yet and I apologies for not explaining clearly. I really need to build an application to kill application processes, Exactly like what you see in the "Applications" tab in Task Manager on Windows 7. "Enumerate Processes" from what I can tell will give me EVERY process like what you will see in the "Processes" tab in Task Manager. That can be dangerous, which is why I don't want my users to use ordinary Task Manager or Process Explorer. Looking into it again, you're right, EnumDesktopWindows does look wrong but EnumWindows might be the way to go to get the "Application" view in Task Manager.

The Applications that will be killed are not mine. They are things like IE or Outlook, which can always crash randomly when under load.
Sergey Alexandrovich Kryukov 17-Jul-13 10:40am    
Well, thank you for this explanation. Still, working with the processes can still be considered, because you could filter our, say, processes not having main window, or by some other criteria...
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900