MultiThread to read Game infomation and remote

03/09/2016 17:37 thfire#1
Hi everyboy I have some problem with Multi Thread and ListView Update:
I want to list running process by name in a Listview, and removing when this process close. But my code just adding a new process and dont remove when the process close. I am a beginner user in C#. Thank you.
*** I dont want to use listview.Clear() cause Im going to make a bot with multi client

Here's my code:
Code:
bool status = true;
int[] PID = new int[10];
Memory Mem = new Memory();
private void startChecking()
        {

            while (status)
            {
                try
                {
                    int count = 0;
                    Process[] processes = Process.GetProcessesByName("notepad");
                    if (processes.Length > 0)
                    {
                        if (listAccount.Items.Count < processes.Length)
                        {
                            foreach (Process process in processes)
                            {
                                if (listAccount.Items.Count < processes.Length && PID[count] != process.Id)
                                {
                                    Mem.SetTitle(process.MainWindowHandle, "New process - " + count.ToString());
                                    AddItemNew(process.MainWindowTitle);
                                    PID[count] = process.Id;
                                }
                                count++;
                            }
                        }
                        else if (listAccount.Items.Count < processes.Length)
                        {

                        }
                    }
                    Thread.Sleep(1000);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Somethine went wrong : " + ex.ToString());
                }
            }
        }
Here is my AddItemNew / RemoveItem methods:

Code:
private delegate void dlgAddItemNew(string i);
        private void AddItemNew(string i)
        {
            if (this.listAccount.InvokeRequired)
            {
                this.Invoke(new dlgAddItemNew(AddItemNew), i);
            }
            else
            {
                ListViewItem accountAdd = new ListViewItem(i);
                accountAdd.SubItems.Add("0");
                accountAdd.SubItems.Add("0");
                accountAdd.SubItems.Add("0");
                accountAdd.SubItems.Add("0");
                this.listAccount.Items.Add(accountAdd);
            }
        }
        private delegate void dlgRemoveItem(int i);
        private void RemoveItem(int i)
        {
            if (this.listAccount.InvokeRequired)
            {
                this.Invoke(new dlgRemoveItem(RemoveItem), i);
            }
            else
            {
                this.listAccount.Items[i].Remove();
            }
        }


P/S : Sorry If my Enlish is not good....