keytrap
keytrap

Reputation: 481

Get real time running processes in WPF checked listbox

Basically I'm having this code which is executed every 2s in a timer :

void GetAllPlayerWindows()
{
        Process[] processes = Process.GetProcessesByName("gamex");

        foreach (Process p in processes)
        {
            IntPtr windowHandle = p.MainWindowHandle;

            if (p.MainWindowTitle != "Login")
            {
                Character player = new Character();
                player.Handle = p.MainWindowHandle;
                player.Name = p.MainWindowTitle;
                lstPlayers.Items.Add(player);
            }
        }
}

This is the Character class:

public class Character
{
    public string Name { get; set; } //MainWindowTitle
    public IntPtr Handle { get; set; } //MainWindowHandle
}

This is my WPF checked listbox

<ListBox x:Name="lstPlayers" ItemsSource="{Binding playerList}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <CheckBox IsChecked="{Binding IsChecked}" Content="{Binding Path=Item.Name}" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

I cannot figure out how to monitor the "gamex" processes and make it coordinate with the listbox. So that if a new process if spawned it creates an item, if it's deleted, it deletes it. As the GetAllPlayerWindows() is executed every 2s it keeps adding every process to the listbox and make duplicates. How to prevent that ?

What I want to achieve :

Having a listbox which always updates and gets the processes that are named "gamex", get theire mainwindowtitle and make sure it's not "Login". Then create a new Character instance and put process Handle & Window Title as Name in there. And put this Character instance in listbox. So that I could access it's items and get the name & handle.

Upvotes: 0

Views: 239

Answers (2)

keytrap
keytrap

Reputation: 481

Ok so I have found my own answer:

void GetAllPlayerWindows()
{
    Process[] processes = Process.GetProcessesByName("gamex");

    foreach (Process p in processes)
    {
        IntPtr windowHandle = p.MainWindowHandle;

        if (p.MainWindowTitle != "Login" && !ContainsHandle(p))//<--Made a little function
        {
            Character player = new Character();
            player.Handle = p.MainWindowHandle;
            player.Name = p.MainWindowTitle;
            lstPlayers.Items.Add(player);
        }
    }
}

bool ContainsHandle(Process p)
    {

        for (int i = 0; i < lstPlayers.Items.Count; i++)
        {
            Character player = lstPlayers.Items[i] as Character;
            if (player.Handle == p.MainWindowHandle)
                return true;
        }
        return false;
    }

Upvotes: 0

rene
rene

Reputation: 42414

Do a Find on your lstPlayers. If it return null the process is not yet in the list, in which case you add it.

void GetAllPlayerWindows()
{
        Process[] processes = Process.GetProcessesByName("gamex");

        foreach (Process p in processes)
        {
            IntPtr windowHandle = p.MainWindowHandle;

            if (p.MainWindowTitle != "Login")
            {
                // find if the handle already is in the list
                if (lstPlayers.Find(i => i.Handle == windowHandle) == null)
                { 
                   // it is not, add it 
                   Character player = new Character();
                   player.Handle = p.MainWindowHandle;
                   player.Name = p.MainWindowTitle;
                   lstPlayers.Items.Add(player);
                }
            }
        }
}

Upvotes: 0

Related Questions