0

With my program I'm trying to automatize another program of which there can be multiple instances. I've already written functionality that will watch the processlist and detect all processes of the program that I want to automatize.

It will store some basic informations about found instances into this ConcurrentDictionary which has its ProcessId as key and the class ProgramToWatch as value:

public static ConcurrentDictionary<int, ProgramToWatch> ProgramToWatchDictionary = new ConcurrentDictionary<int, ProgramToWatch>();

public class ProgramToWatch
{
    public string ListItemName { get; set; }
    public Process BEProcess { get; set; }
    public string BEMainWindowTitle { get; set; }
    public Application BEApplication { get; set; }
    public Window BEWindow { get; set; }
    public bool isLoggedIn { get; set; }

    public List<ProgramToWatchDataGrid> BEDataGrid = new List<ProgramToWatchDataGrid>();
}

Now the part I am having problems with. The program I want to watch has a DataGridView which I want to copy into my dictionary. For this I have the BEDataGrid List. The list is using this class as its type:

public class ProgramToWatchDataGrid
{
    public int ListID { get; set; }
    public string Name { get; set; }
    public string Mail { get; set; }
}

Now to store it, I create another instance of the ProgramToWatchDataGrid (called updateGrid), write all the data I read into it, and place this into my Dictionary (I simplified). To do this, I iterate through the DataGrid (first while loop), row for row and copy the updateGrid to my Dictionary - in the second while loop I display the values to verify:

public void ReadDataGridView(int ProcessId)
{
    ProgramToWatchDataGrid updateGrid = new ProgramToWatchDataGrid();

    //read and store every row
    int i=0;
    while(i<=totalRowsInGrid)
    {
        updateGrid.ListId = DataGrid.Rows[i].Cells[0].Value;
        updateGrid.Name = DataGrid.Rows[i].Cells[1].Value;
        updateGrid.Mail = DataGrid.Rows[i].Cells[2].Value;
        ProgramToWatchDictionary[ProcessID].BEDataGrid.Insert(i, updateGrid);
        Display(ProgramToWatchDictionary[ProcessID].BEDataGrid[i].Mail);
    }

    Display("Elements: " + ProgramToWatchDictionary[ProcessID].BeDataGrid.Count);

    //display every rows mail
    i=0;
    while(i<=totalRowsInGrid)
    {
        Display("HI: " + ProgramToWatchDictionary[ProcessID].BEDataGrid[i].Mail);
        i++;
    }
}

The way I understand it, the Information rows I read should now be located in ProgramToWatchDictionary[ProcessID].BEDataGrid[accordingRow] because I inserted the Information at that place.

The strange thing is, that this output will be produced:

[01:19] christoferlindstrm@yahoo.com
[01:19] eliseisaksson@yahoo.com
[01:19] peter@pan.com
[01:19] Elements: 3
[01:19] HI: peter@pan.com
[01:19] HI: peter@pan.com
[01:19] HI: peter@pan.com

So right after I've inserted it, the List contains the right value. But after it will always be the last value that was read? Why does this happen and how can I fix this?

Some help would be greatly appreciated!

4

1 回答 1

0

知道了。您应该同时创建要添加的对象的实例,否则您只使用一个实例,而该实例恰好在您的循环中获得不同的值。当然,它会以您插入的最后一个值结束。

public void ReadDataGridView(int ProcessId)
{
    ProgramToWatchDataGrid updateGrid = null;

    //read and store every row
    int i=0;
    while(i<=totalRowsInGrid)
    {
        //create a new instance
        updateGrid = new ProgramToWatchDataGrid();
        updateGrid.ListId = DataGrid.Rows[i].Cells[0].Value;
        updateGrid.Name = DataGrid.Rows[i].Cells[1].Value;
        updateGrid.Mail = DataGrid.Rows[i].Cells[2].Value;

        ProgramToWatchDictionary[ProcessID].BEDataGrid.Insert(i, updateGrid);
        Display(ProgramToWatchDictionary[ProcessID].BEDataGrid[i].Mail);
    }

    Display("Elements: " + ProgramToWatchDictionary[ProcessID].BeDataGrid.Count);

    //display every rows mail
    i=0;
    while(i<=totalRowsInGrid)
    {
        Display("HI: " + ProgramToWatchDictionary[ProcessID].BEDataGrid[i].Mail);
        i++;
    }
}
于 2013-09-16T23:51:38.190 回答