Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


C# Add list of visible apps in a ListView

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Krähne
Expert Cheater
Reputation: 0

Joined: 06 Jun 2010
Posts: 108
Location: Inside of my Kernel

PostPosted: Tue Mar 08, 2011 2:25 pm    Post subject: C# Add list of visible apps in a ListView Reply with quote

Hi there... i have made this code for verify the list of visible apps in taskbar, but... i don't know if this is the correct way to make it...

So... here i'll paste the snippet, and... You judge:

Code:
public int GetWindowsRunning()
        {
            int RWindows = 0;
            ListView LV = new ListView();

            foreach (Process Procesess in Process.GetProcesses("."))
            {
                try
                {
                    if (Procesess.MainWindowTitle.Length > 0)
                    {
                        string VisibleWindows = Procesess.MainWindowTitle.ToString();
                        ListViewItem LVItem = new ListViewItem(VisibleWindows);
                        LV.Items.Add(LVItem);
                    }
                }
                catch { }
            }

            RWindows = LV.Items.Count;
            return RWindows;
        }


Please, if exists another way to check this, can yours show me? Very Happy

Thanks in advance!

Gruß.
Back to top
View user's profile Send private message MSN Messenger
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8585
Location: 127.0.0.1

PostPosted: Tue Mar 08, 2011 3:02 pm    Post subject: Reply with quote

If the ListView is already created by you dragging and dropping one onto the forum, don't create a new one inside the function. Just use the one you created already. If you just need the window title of the process or the process name itself just use a ListBox as well.

You can use Linq to easily do the above too like this:
This again, using a ListBox instead of a ListView just to display the titles.
Code:
        private int GetWindowsRunning()
        {
            try
            {
                var processes = from proc in Process.GetProcesses()
                                where proc.MainWindowTitle.Length > 0
                                select proc.MainWindowTitle;

                foreach (string title in processes)
                {
                    listBox1.Items.Add(title);
                }

                return processes.ToArray().Length;
            }
            catch { return 0; }
        }
    }


You could also use the DataSource for the ListBox and bind it directly like this:
Code:
        private int GetWindowsRunning()
        {
            try
            {
                listBox1.DataSource = (from proc in Process.GetProcesses()
                                       where proc.MainWindowTitle.Length > 0
                                       select proc.MainWindowTitle).ToArray();
               
                return listBox1.Items.Count;
            }
            catch { return 0; }
        }

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
AhMunRa
Grandmaster Cheater Supreme
Reputation: 27

Joined: 06 Aug 2010
Posts: 1117

PostPosted: Tue Mar 08, 2011 3:55 pm    Post subject: Reply with quote

Wiccan, don't you still have to create the new Item before you can add it?

Nevermind, I looked it up.

_________________
<Wiccaan> Bah that was supposed to say 'not saying its dead' lol. Fixing >.>
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8585
Location: 127.0.0.1

PostPosted: Tue Mar 08, 2011 4:07 pm    Post subject: Reply with quote

AhMunRa wrote:
Wiccan, don't you still have to create the new Item before you can add it?

Nevermind, I looked it up.


For a ListView yes, but he also created the ListView itself inside the function which isn't needed if he already created one. Since he is creating it like that inside the function as well, it will be out of scope as soon as the function returns making it a useless control.

For list boxes, you don't need to create an item since it expects nothing but a string for each item.

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
AhMunRa
Grandmaster Cheater Supreme
Reputation: 27

Joined: 06 Aug 2010
Posts: 1117

PostPosted: Tue Mar 08, 2011 4:16 pm    Post subject: Reply with quote

Yeah, I saw where you mentioned him calling it in the function and saw that. I've not done anything using ListView, have used gridview, and listbox.

Thank you for the explanation.

_________________
<Wiccaan> Bah that was supposed to say 'not saying its dead' lol. Fixing >.>
Back to top
View user's profile Send private message
Krähne
Expert Cheater
Reputation: 0

Joined: 06 Jun 2010
Posts: 108
Location: Inside of my Kernel

PostPosted: Tue Mar 08, 2011 4:32 pm    Post subject: Reply with quote

Wiccaan wrote:
If the ListView is already created by you dragging and dropping one onto the forum, don't create a new one inside the function. Just use the one you created already. If you just need the window title of the process or the process name itself just use a ListBox as well.

You can use Linq to easily do the above too like this:
This again, using a ListBox instead of a ListView just to display the titles.
Code:
        private int GetWindowsRunning()
        {
            try
            {
                var processes = from proc in Process.GetProcesses()
                                where proc.MainWindowTitle.Length > 0
                                select proc.MainWindowTitle;

                foreach (string title in processes)
                {
                    listBox1.Items.Add(title);
                }

                return processes.ToArray().Length;
            }
            catch { return 0; }
        }
    }


You could also use the DataSource for the ListBox and bind it directly like this:
Code:
        private int GetWindowsRunning()
        {
            try
            {
                listBox1.DataSource = (from proc in Process.GetProcesses()
                                       where proc.MainWindowTitle.Length > 0
                                       select proc.MainWindowTitle).ToArray();
               
                return listBox1.Items.Count;
            }
            catch { return 0; }
        }


no no wait... i use that method (create a virtual listview) because i don't have find another way to enum the processwindows, but... i saw you snippet and i learn something xD... i mean, i was don't know the "processes.ToArray().Length;"...

So... for that i don't use my created listview...

Anyways... using my method everything works, but... with one detail... my form gets lag!... And... for that i'm here asking help... you snippet also makes my form gets lag.

The fact here, is that i need to make a function that returns a int value, that value, is the number of visible apps. I don't need to add the window names, just need the number of visible apps, just that! :/.

for add the name windows to my listview, i made this snippet (also contains the process id):

Code:
public static void UpdateAppWindowsList(ListView LV)
        {
            foreach (Process Procesess in Process.GetProcesses("."))
            {
                try
                {
                    if (Procesess.MainWindowTitle.Length > 0)
                    {
                        string[] Windows = { Procesess.MainWindowTitle.ToString(), Procesess.Id.ToString() };
                        ListViewItem ProcessItems = new ListViewItem(Windows);
                        LV.Items.Add(ProcessItems);
                    }
                }
                catch { }
            }
        }


I also made another function to get the number of running process, is more easy and with this way, my form no gets lag... but when i use the other function (my or you function) for get the app visible list, my form gets lag ...

Here's my function:

Code:
public int GetProcessRunning()
        {
            Process[] ProcessCount = Process.GetProcesses(".");
            return Convert.ToInt32(ProcessCount.Length);
        }


Well, i'll try with other methods, but... if you have another idea, i will appreciate it!.

Gruß.

PD: Sorry if you don't get it all i've say... but my english is so basic... xD
Back to top
View user's profile Send private message MSN Messenger
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8585
Location: 127.0.0.1

PostPosted: Tue Mar 08, 2011 6:56 pm    Post subject: Reply with quote

Whenever you are dealing with populating a control with content, you are going to see lag depending on how much data is being populated. For a list view, this is fairly common. The best thing you can do is tell the application to continue working while the list view is being populated, and lock the control until it is finished.

If not, you will need to deal with the lag. For example a ListView setup for Details with 3 columns can be added to like this:

Code:
        /// <summary>
        /// Updates the running process list view and
        /// returns the number of processes found.
        /// </summary>
        /// <param name="lv"></param>
        /// <returns></returns>
        public int UpdateAppList(ListView lv)
        {
            try
            {
                var lvItems = from proc in Process.GetProcesses()
                              where proc.MainWindowTitle.Length > 0
                              select new ListViewItem(new string[] { proc.ProcessName, proc.MainWindowTitle, proc.Id.ToString() });

                lv.Items.AddRange(lvItems.ToArray());
                return lvItems.ToArray().Length;
            }
            catch { return 0; }
        }


The setup code for it (you can just use the properties to change it etc.):
Code:
        public Form1()
        {
            InitializeComponent();

            listView1.View = View.Details;
            listView1.Columns.Add("Process Name", 175);
            listView1.Columns.Add("Process Window", 175);
            listView1.Columns.Add("Process Id", 75);

            int nCount = UpdateAppList(this.listView1);
            this.Text = "Processes found: " + nCount.ToString();
        }


This is instant for me with 4 windows appearing. Granted higher numbers will result in lag, and if you are constantly calling it, you will want to probably thread it. (Keep in mind you will have to use invoke calls then since you cannot access the controls cross-thread.)

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
Krähne
Expert Cheater
Reputation: 0

Joined: 06 Jun 2010
Posts: 108
Location: Inside of my Kernel

PostPosted: Wed Mar 09, 2011 1:15 am    Post subject: Reply with quote

Goddamn!, you still don't understand; i don't... BAH forget it. (With all due respect clear...)

Please, delete this post, when you have done, i will remake another with the correct question.

And... sorry for this spam (?), i promess in the next thread, i will explain better my question.

Gruß.

EDIT:
_______________________________________________________

Well... as you don't delete this thread, here i post the answer.

The way for do that without that forms gets lag... Is checking the list of visible apps in a new thread.

That's it... making a new thread, everything works correctly.

Thanks anyways.

Gruß.
Back to top
View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites