views:

35

answers:

2

Hello,

I would like to add rows to DataGridView from two seperate threads. I tried something with delegates and BeginInvoke but doesn't work.

Here is my row updater function which is called from another function in a thread.

    public delegate void GRIDLOGDelegate(string ulke, string url, string ip = "");
    private void GRIDLOG(string ulke, string url, string ip = "")
    {

        if (this.InvokeRequired)
        {
            // Pass the same function to BeginInvoke,
            // but the call would come on the correct
            // thread and InvokeRequired will be false.
            object[] myArray = new object[3];

            myArray[0] = ulke;
            myArray[1] = url;
            myArray[2] = ip;

            this.BeginInvoke(new GRIDLOGDelegate(GRIDLOG),
                                             new object[] { myArray });

            return;
        }

        //Yeni bir satır daha oluştur
        string[] newRow = new string[] { ulke, url, ip };
        dgLogGrid.Rows.Add(newRow);
    }
A: 
this.BeginInvoke(new GRIDLOGDelegate(GRIDLOG),
        //error seems to be here -> new object[] { myArray });
        myArray) // <- how it should be

Update:

You can also do it this way:

BeginInvoke(new GRIDLOGDelegate(GRIDLOG), ulke, url, ip);
max
A: 

U need to pass array of parameters. U r making mistake while calling this.BeginInvoke

Try like this:

this.BeginInvoke(new GRIDLOGDelegate(GRIDLOG), new object[] { ulke, url, ip });

Everything else seems correct.