>
windows mobile task manager
Welcome, Guest

by dhanendra on Tuesday, 10 March 2009
windows mobile task manager


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using OpenNETCF.ToolHelp;
using System.IO;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Microsoft.WindowsCE.Forms;
using System.Xml;

namespace MobileTaskManager
{
    public partial class frmMobileTaskManager : Form
    {
        public class TaggedListViewSubItem : ListViewItem.ListViewSubItem
        {
            public uint Tag { get; set; }

            public TaggedListViewSubItem(string text, uint tag)
            {
                Text = text;
                Tag = tag;
            }
        }

        [DllImport("coredll.dll", CharSet = CharSet.Auto, SetLastError = false)]
        static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

        [DllImport("coredll.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

        [DllImport("coredll.dll")]
        static extern int GetSystemMetrics(int nIndex);

        [DllImport("coredll.dll")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);

        IntPtr INVALID_HANDLE_VALUE = (IntPtr)(-1);

        const uint IOCTL_KLIB_GETPROCMEMINFO = 8;
        const uint WDAPI_CREATE = 0;
        const uint WDAPI_START = 2;
        const uint IOCTL_KLIB_WDOGAPI = 19; // watchdog timer APIs
        const int KMOD_CORE = 1; //

        const int SM_CXVSCROLL = 2;

        int m_ScrollbarWidth = 0;
        static int s_SortColumn = 0;
        static int s_Ascending = 1;

        string m_AppSettingsPath = String.Empty;
        List<ListViewItem> m_ListViewItems = null;

        public frmMobileTaskManager()
        {
            InitializeComponent();
            MobileDevice.Hibernate += new EventHandler(MobileDevice_Hibernate);
        }

        void MobileDevice_Hibernate(object sender, EventArgs e)
        {
            lvTasks.Items.Clear();
        }

        private void frmMobileTaskManager_Load(object sender, EventArgs e)
        {
            m_AppSettingsPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            if (m_AppSettingsPath.EndsWith("\") == false)
                m_AppSettingsPath += "\";
            m_AppSettingsPath += "MobileTaskManager.xml";

            if (File.Exists(m_AppSettingsPath) == false)
            {
                using(StreamWriter sw = new StreamWriter(m_AppSettingsPath))
                    sw.Write("<Settings><LeftSoftKey>Kill</LeftSoftKey></Settings>");
            }

            XmlDocument Settings = new XmlDocument();
            Settings.Load(m_AppSettingsPath);
            miAction.Text = Settings.SelectSingleNode("//LeftSoftKey").InnerText;

            Show();
            m_ScrollbarWidth = GetSystemMetrics(SM_CXVSCROLL) + 2;
            chImageName.Width = chWindowName.Width = ((ClientSize.Width - m_ScrollbarWidth) / 2);
        }
        
        uint GetMemUsage(uint ProcId)
        {
            uint MemUsage = 0;
            IntPtr hHeapSnapshot = Toolhelp32.CreateToolhelp32Snapshot(Toolhelp32.TH32CS_SNAPHEAPLIST, ProcId);
            if (hHeapSnapshot != INVALID_HANDLE_VALUE)
            {
                Toolhelp32.HEAPLIST32 HeapList = new Toolhelp32.HEAPLIST32();

                HeapList.dwSize = (uint)Marshal.SizeOf(HeapList);

                if (Toolhelp32.Heap32ListFirst(hHeapSnapshot, ref HeapList))
                {
                    do
                    {
                        Toolhelp32.HEAPENTRY32 HeapEntry = new Toolhelp32.HEAPENTRY32();
                        HeapEntry.dwSize = (uint)Marshal.SizeOf(HeapEntry);

                        if (Toolhelp32.Heap32First(hHeapSnapshot, ref HeapEntry, HeapList.th32ProcessID, HeapList.th32HeapID))
                        {
                            do
                            {
                                MemUsage += HeapEntry.dwBlockSize;
                            } while (Toolhelp32.Heap32Next(hHeapSnapshot, ref HeapEntry));
                        }
                    } while (Toolhelp32.Heap32ListNext(hHeapSnapshot, ref HeapList));
                }
                Toolhelp32.CloseToolhelp32Snapshot(hHeapSnapshot);
            }
            return MemUsage;
        }

        string FormatMemoryString(uint Mem)
        {
            string Unit = "B";

            if (Mem >= 1024)
            {
                Mem /= 1024;
                Unit = "K";
            }

            return String.Format("{0:N0} {1}", Mem, Unit);
        }

        void RefreshList()
        {
            lvTasks.Items.Clear();

            ProcessEntry[] pes = ProcessEntry.GetProcesses();

            if (m_ListViewItems == null)
            {
                m_ListViewItems = new List<ListViewItem>(1);

                foreach (ProcessEntry pe in pes)
                {
                    if (Path.GetFileName(pe.ExeFile).ToLower() == "gps monitor.exe")
                    {


                        Process p = Process.GetProcessById((int)pe.ProcessID);
                        uint RWMemUsed = GetMemUsage(pe.ProcessID);
                        ListViewItem lvi = null;
                        string WindowText = String.Empty;

                        try
                        {
                            if (p.MainWindowHandle != IntPtr.Zero)
                            {
                                StringBuilder sb = new StringBuilder(255);
                                GetWindowText(p.MainWindowHandle, sb, 255);
                                WindowText = sb.ToString();
                            }
                        }
                        catch (Exception ex)
                        {
                            string s = ex.ToString();
                        }

                        if (lvi == null)
                            lvi = new ListViewItem(Path.GetFileName(pe.ExeFile));

                        lvi.SubItems.Add(new TaggedListViewSubItem(FormatMemoryString(RWMemUsed), RWMemUsed));
                        lvi.SubItems.Add(WindowText);

                        lvi.Tag = pe;
                        m_ListViewItems.Add(lvi);

                        Application.DoEvents();


                    }
                    
                }
            }

            m_ListViewItems.Sort(CompareListViewItems);

            foreach (ListViewItem lvi in m_ListViewItems)
            {
                
                lvTasks.Items.Add(lvi);

            }

            if (lvTasks.Items.Count > 0)
                lvTasks.Items[0].Selected = true;

            lvTasks.Focus();
        }

        private static int CompareListViewItems(ListViewItem lvi1, ListViewItem lvi2)
        {
            if (s_SortColumn == 1)
            {
                uint val1 = ((TaggedListViewSubItem)lvi1.SubItems).Tag;
                uint val2 = ((TaggedListViewSubItem)lvi2.SubItems).Tag;

                if(val1 > val2)
                    return 1 * s_Ascending;
                else if (val1 < val2)
                    return -1 * s_Ascending;
                else
                    return 0;
            }
            else
                return String.Compare(lvi1.SubItems.Text, lvi2.SubItems.Text) * s_Ascending;
        }

        private void miRefresh_Click(object sender, EventArgs e)
        {
            m_ListViewItems.Clear();
            m_ListViewItems = null;
            RefreshList();
        }

        private void miKill_Click(object sender, EventArgs e)
        {
            if (lvTasks.SelectedIndices.Count > 0)
            {
                ListViewItem lvi = lvTasks.Items[lvTasks.SelectedIndices[0]];

                if (MessageBox.Show(String.Format("Are you sure you wish to terminate {0}?", lvi.Text), "Confirm Kill", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
                {

                    ProcessEntry pe = (ProcessEntry)lvi.Tag;

                    try
                    {
                        try
                        {
                            Process p = Process.GetProcessById((int)pe.ProcessID);

                            if (p.MainWindowHandle != IntPtr.Zero)
                                SendMessage(p.MainWindowHandle, (uint)OpenNETCF.Win32.WM.CLOSE, IntPtr.Zero, IntPtr.Zero);
                            System.Threading.Thread.Sleep(500);
                            if (Process.GetProcessById((int)pe.ProcessID) != null)
                                pe.Kill();
                        }
                        catch (ArgumentException) { }

                        lvTasks.Items.Remove(lvi);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                }
            }
        }

        private void miExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void frmMobileTaskManager_Activated(object sender, EventArgs e)
        {
            if (lvTasks.Items.Count == 0)
                RefreshList();
        }

        private void frmMobileTaskManager_Resize(object sender, EventArgs e)
        {
            chImageName.Width = chWindowName.Width = ((ClientSize.Width - m_ScrollbarWidth) / 2);
        }

        private void lvTasks_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            if (s_SortColumn == e.Column)
                s_Ascending *= -1;
            else
            {
                s_SortColumn = e.Column;
                s_Ascending = 1;
            }

            RefreshList();
        }

        private void miActivate_Click(object sender, EventArgs e)
        {
            if (lvTasks.SelectedIndices.Count > 0)
            {
                ListViewItem lvi = lvTasks.Items[lvTasks.SelectedIndices[0]];
                ProcessEntry pe = (ProcessEntry)lvi.Tag;

                try
                {
                    Process p = Process.GetProcessById((int)pe.ProcessID);

                    if (p.MainWindowHandle != IntPtr.Zero)
                        SetForegroundWindow(p.MainWindowHandle);
                }
                catch (ArgumentException) { }
            }
        }

        private void lvTasks_SelectedIndexChanged(object sender, EventArgs e)
        {
            //if (lvTasks.SelectedIndices.Count > 0)
            //{
            //    ListViewItem lvi = lvTasks.Items[lvTasks.SelectedIndices[0]];
            //    ProcessEntry pe = (ProcessEntry)lvi.Tag;

            //    try
            //    {
            //        Process p = Process.GetProcessById((int)pe.ProcessID);

            //        if (p.MainWindowHandle == IntPtr.Zero)
            //            miActivate.Enabled = false;
            //        else
            //            miActivate.Enabled = true;
            //    }
            //    catch (ArgumentException) { }
            //}

        }

        
        private void miAction_Click(object sender, EventArgs e)
        {
            switch (miAction.Text)
            {
                case "Kill":
                    miKill_Click(sender, e);
                    break;
                
            }
        }

        
        
    }
}



Blogger Image
My Blog Title

Product Engineering, software engineering company, Product Development, Product Migration, Product Re-engineering, Product Maintenance, Product Testing Commercial Application Development, Business Software development, commercial software for startups, Application Support and Maintenance, software testing Product Maintenance, Outsource product maintenance, product support and maintenance Product Migration, Product Re-engineering, product re-engineering services Product Research, Product Engineering, UI Prototyping Services Software Testing Services, Quality Assurance services, professional software testers, Load Testing, Functional Testing, Cross Platform, Browser Testing, Test Automation, Testing Tools, software quality analysis Functional Testing Services, software quality analysis, Software Testing Services, Application Testing Services, Functional Testing Types Automated Testing, Automated Testing Services, automation testing, test script development, Automation Test Tools, outsource automation testing Load Testing, Performance Testing Services, Load Testing Tools Offshore Software Development, Outsource software services, offshore outsourcing services, offshore software development services, IT outsourcing services, software quality assurance services, Offshore IT services, Custom Application Development Services, Offshore Product Engineering Benefits of IT Outsourcing, Offshore Software Development companies, offshore software development firms Outsource planning, IT outsourcing, IT development services, offshore IT companies, offshore software development Offshore Software Development, Outsource software services, offshore outsourcing services, offshore software development services, IT outsourcing services, software quality assurance services, Offshore IT services, Custom Application Development Services, Offshore Product Engineering Offshore Software Development, Outsource software services, offshore outsourcing services, offshore software development services, IT outsourcing services, software quality assurance services, Offshore IT services, Custom Application Development Services, Offshore Product Engineering