AllExperts > Experts 
Search      

VB.NET

Volunteer
Answers to thousands of questions
 Home · More Questions · Answer Library  · Encyclopedia ·
More VB.NET Answers
Question Library

Ask a question about VB.NET
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Chris
Expertise
I can answer pretty much any question relating to VB.NET and its use in a Windows environment. I specialize in ASP.NET web development and MSSQL database access.

Experience
I have over 5 years of industry experience using VB.NET and other .NET technologies for web and database development.

Education/Credentials
I have some college education, but does it really matter in this field of work?

 
   

You are here:  Experts > Computing/Technology > Basic > VB.NET > Reading Active Applications from Task Manager.

Topic: VB.NET



Expert: Chris
Date: 9/12/2007
Subject: Reading Active Applications from Task Manager.

Question
Hi,
I need to read the active applications from task manager(Applications tab) and populate it into a list box.On clicking the value in list box the particular application should become active.I am using Visual studio 2003.
Any help will be appreciated.

Thanks,
Suhani

Answer
Hi,

As far as I know, you can only do this with programs that have a MainWindowHandle.  Most of the programs you see in the Applications list in Task Manager do not have a main window, or at least they are hidden from the .NET Framework.

For those that do provide a window handle, it's relatively easy to do what you want.  Create a new Form (I named it Form1) and use the following code, it should get you started:

'--[ begin Form1.vb ]--
Imports System.Diagnostics

Public Class Form1
 Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
   If Not (ListBox1.SelectedItem Is Nothing) Then
     Dim objProcess As MyProcInfo = DirectCast(ListBox1.SelectedItem, MyProcInfo)

     If Not (objProcess.ProcInfo.MainWindowHandle = IntPtr.Zero) Then
       Dim hWnd As IntPtr = objProcess.ProcInfo.MainWindowHandle
       If IsIconic(hWnd) Then ShowWindowAsync(hWnd, SW_RESTORE)
       SetForegroundWindow(hWnd)
     End If
   End If
 End Sub

 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
   Dim arrProcesses As Process() = Process.GetProcesses()
   ListBox1.Items.Clear()

   For i As Integer = 0 To arrProcesses.Length - 1
     Dim objProcess As Process = arrProcesses(i)
     If Not (objProcess.MainWindowHandle = IntPtr.Zero) Then ListBox1.Items.Add(New MyProcInfo(objProcess))
   Next
 End Sub

 Private Class MyProcInfo
   Public ProcInfo As Process

   Private Sub New()
   End Sub

   Public Sub New(ByVal objProcInfo As Process)
     ProcInfo = objProcInfo
   End Sub

   Public Overrides Function ToString() As String
     Return DirectCast(IIf((ProcInfo.MainWindowHandle = IntPtr.Zero), "", "* "), String) & ProcInfo.ProcessName & " (" & ProcInfo.Id & ")"
   End Function
 End Class
End Class
'--[ end Form1.vb ]--

You'll also need to add a new code Module to your project, with some API call wrappers you'll need to get these process windows to show themselves and be set to the foreground:

'--[ begin APICalls.vb ]--
Imports System.Runtime.InteropServices

Module APICalls
 <DllImport("user32.dll")> _
 Function SetForegroundWindow(ByVal hWnd As IntPtr) As Boolean
 End Function

 <DllImport("user32.dll")> _
 Function ShowWindowAsync(ByVal hWnd As IntPtr, ByVal nCmdShow As Integer) As Boolean
 End Function

 <DllImport("user32.dll")> _
 Function IsIconic(ByVal hWnd As IntPtr) As Boolean
 End Function

 Public Const SW_HIDE As Integer = 0
 Public Const SW_SHOWNORMAL As Integer = 1
 Public Const SW_SHOWMINIMIZED As Integer = 2
 Public Const SW_SHOWMAXIMIZED As Integer = 3
 Public Const SW_SHOWNOACTIVATE As Integer = 4
 Public Const SW_RESTORE As Integer = 9
 Public Const SW_SHOWDEFAULT As Integer = 10
End Module
'--[ end APICalls.vb ]--

Hope this helps!

Add to this Answer    Ask a Question



  Rate this Answer
   Was this answer helpful?
Not at allDefinitely              
   12345  

     
About Us | Advertise on This Site | User Agreement | Privacy Policy | Help
Copyright  © 2008 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.