6

During the installation of any application. Generally the user was asked to close all windows before start of installation. if not the installation will stop at the middle and ask the user to close all opened windows. I was asked to add a code in a XXX application. When application is running and if user opened any other window (ex: Explore, browser, word etc..) then the application should throw a window saying that you have opened the list of windows. I requerst you please suggest me how to start in C#.

user517206
  • 151
  • 2
  • 2
  • 6

4 Answers4

7

Test this:

var openWindowProcesses = System.Diagnostics.Process.GetProcesses()
   .Where(p => p.MainWindowHandle != IntPtr.Zero && p.ProcessName != "explorer");

The openWindowProcesses should contains all open application which they have an active main window.

I put p.ProcessName != "explorer" in the where expression because the explorer is the main process of the Desktop and it should never be closed.

To watching execution of the processes you can use ManagementEventWatcher class. See this please.

Jeremy J Starcher
  • 23,369
  • 6
  • 54
  • 74
Amir Karimi
  • 5,401
  • 4
  • 32
  • 51
  • To solve described problem you need to get window list by timer. And what's about hidden windows? – DReJ Dec 15 '10 at 15:23
  • I think who asked this question knows about using timers and ... And about hidden windows; I think it's enough to close processes with unhidden windows in such scenarios. Although in these situation the processes are more important than windows. Programmer should check the processes to find out which one should not be open. This is easily possible by changing the expression in the Where method. But thanks! I will be add some info about watching processes in my answer. – Amir Karimi Dec 15 '10 at 15:35
  • thank a lot for your suggestion. Once I implement i will let you know the result – user517206 Dec 16 '10 at 05:57
  • What if an application has multiple visible windows? – Victor Zakharov Dec 29 '15 at 17:07
1

You can use System.Diagnostics.Process class to get the information of all processes which are running in your machine. Then you can try to find if intended application/process is running or not.

You can either use GetProcesses() or GetProcessByName() method. Refer this msdn link for reference. I am sure that there can be more efficient way of achieving the same. HTH

Pradeep
  • 3,258
  • 1
  • 23
  • 36
1

Set up a foreach loop like this to enumerate over all the open applications on your system (that have a visible main window)

foreach (var process in Process.GetProcesses().Where( p => p.MainWindowHandle != IntPtr.Zero)) {
    //do something with the process here. To display it's name, use process.MainWindowTitle
  }
Øyvind Bråthen
  • 59,338
  • 27
  • 124
  • 151
0

Try SetWinEventHook WinAPI function via P/Invoke. You can hook EVENT_OBJECT_CREATE event, but I am not 100% sure. I have never done it in C#, only in C/C++.

DReJ
  • 1,966
  • 15
  • 14