C#从另一个线程更改控件,调用不起作用

时间:2014-03-18 13:26:25

标签: c# multithreading tcpclient

这是我的代码:

public partial class Form1 : Form
{

    void ThreadProc(string data)
    {
        int i;

        Console.WriteLine("Received: {0}", data);

        this.Invoke((MethodInvoker)delegate()
        {

            string[] tok = data.Split('|');

            int rowIndex = 10000 * 10000;

            foreach (DataGridViewRow row in UsersBets.Rows)
            {
                if ((row.Cells[1].Value != null) && (row.Cells[1].Value.ToString().Equals(tok[0])))
                {
                    if (row.Index < rowIndex) rowIndex = row.Index;
                }
            }



            if (rowIndex != 10000 * 10000)
            {
                DataGridViewRow row = UsersBets.Rows.SharedRow(rowIndex);
                string user = row.Cells[0].Value.ToString();

                TreeNode[] tnarr = treeView1.Nodes.Find(tok[0], false);
                if (tnarr[0] != null)
                {
                    this.UsersBets.Rows.Insert(rowIndex + 1, null, tok[0], null, tok[1], tok[2], null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, System.DateTime.Now);


                    if (this.UsersBets.Rows[rowIndex].Cells[0].Value.Equals("-"))
                    {
                        this.UsersBets.Rows[rowIndex + 1].Visible = true;
                    }
                    else
                    {
                        this.UsersBets.Rows[rowIndex + 1].Visible = false;
                    }
                    this.UsersBets.Rows[rowIndex + 1].Cells[1].Style.ForeColor = Color.White;
                    tnarr[0].Nodes.Insert(0, tok[1]);


                }

            }
            else
            {
                rowIndex = 0;

                this.UsersBets.Rows.Insert(rowIndex, "+", tok[0], null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null);

                this.UsersBets.Rows.Insert(rowIndex + 1, null, tok[0], null, tok[1], tok[2], null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, System.DateTime.Now);

                if (this.UsersBets.Rows[rowIndex].Cells[0].Value.Equals("-"))
                {
                    this.UsersBets.Rows[rowIndex + 1].Visible = true;
                }
                else
                {
                    this.UsersBets.Rows[rowIndex + 1].Visible = false;
                }

                this.UsersBets.Rows[rowIndex + 1].Cells[1].Style.ForeColor = Color.White;

            }

            int max = 0;

            for (i = 3; i < tok.Length - 1; i++)
            {
                if (Convert.ToInt32(tok[i]) == 0)
                {
                    this.UsersBets.Rows[rowIndex + 1].Cells[i + 2].Style.BackColor = System.Drawing.Color.Green;
                }
                else if ((Convert.ToInt32(tok[i]) > 10) && (Convert.ToInt32(tok[i]) < 20))
                {
                    this.UsersBets.Rows[rowIndex + 1].Cells[i + 2].Style.BackColor = System.Drawing.Color.Blue;
                }
                else if (Convert.ToInt32(tok[i]) == 20)
                {
                    this.UsersBets.Rows[rowIndex + 1].Cells[i + 2].Style.BackColor = System.Drawing.Color.Yellow;
                }
                else if ((Convert.ToInt32(tok[i]) > 30) && (Convert.ToInt32(tok[i]) < 40))
                {
                    this.UsersBets.Rows[rowIndex + 1].Cells[i + 2].Style.BackColor = System.Drawing.Color.Red;
                }
            }

            switch (max)
            {
                case 0:
                    this.UsersBets.Rows[rowIndex + 1].Cells[2].Style.BackColor = System.Drawing.Color.LimeGreen;
                    break;


                case 1:
                    this.UsersBets.Rows[rowIndex + 1].Cells[2].Style.BackColor = System.Drawing.Color.Yellow;
                    break;

                case 2:
                    this.UsersBets.Rows[rowIndex + 1].Cells[2].Style.BackColor = System.Drawing.Color.Red;
                    break;

                case 3:
                    this.UsersBets.Rows[rowIndex + 1].Cells[2].Style.BackColor = System.Drawing.Color.Blue;
                    break;
            }

            tok = tok[tok.Length - 1].Split(';');

            max = Convert.ToInt32(tok[0]);

            for (i = 0; i < tok.Length; i++)
            {
                if (Convert.ToInt32(tok[i]) == 0)
                {
                    this.UsersBets.Rows[rowIndex + 1].Cells[i + 2].Style.BackColor = System.Drawing.Color.Green;
                }
                else if ((Convert.ToInt32(tok[i]) > 10) && (Convert.ToInt32(tok[i]) < 20))
                {
                    this.UsersBets.Rows[rowIndex + 1].Cells[i + 2].Style.BackColor = System.Drawing.Color.Blue;
                }
                else if (Convert.ToInt32(tok[i]) == 20)
                {
                    this.UsersBets.Rows[rowIndex + 1].Cells[i + 2].Style.BackColor = System.Drawing.Color.Yellow;
                }
                else if ((Convert.ToInt32(tok[i]) > 30) && (Convert.ToInt32(tok[i]) < 40))
                {
                    this.UsersBets.Rows[rowIndex + 1].Cells[i + 2].Style.BackColor = System.Drawing.Color.Red;
                }
            }

            switch (max)
            {
                case 0:
                    this.UsersBets.Rows[rowIndex].Cells[2].Style.BackColor = System.Drawing.Color.LimeGreen;
                    break;


                case 1:
                    this.UsersBets.Rows[rowIndex].Cells[2].Style.BackColor = System.Drawing.Color.Yellow;
                    break;

                case 2:
                    this.UsersBets.Rows[rowIndex].Cells[2].Style.BackColor = System.Drawing.Color.Red;
                    break;

                case 3:
                    this.UsersBets.Rows[rowIndex].Cells[2].Style.BackColor = System.Drawing.Color.Blue;
                    break;
            }


        });


    }


    public Form1()
    {
        InitializeComponent();
    }



    private void button1_Click_1(object sender, EventArgs e)
    {

        // Set the TcpListener on port 12000.
        Int32 port = 12000;
        IPAddress localAddr = IPAddress.Parse("110.169.128.289");

        Console.Write("Waiting for a connection... ");
        string data = null;
        // Perform a blocking call to accept requests. 
        // You could also user server.AcceptSocket() here.

        TcpClient client = new TcpClient("192.168.118.145", port);

        try
        {
            //Buffer for reading data
            Byte[] bytes = new Byte[256];

            Console.WriteLine("Connected!");
            // Get a stream object for reading and writing
            NetworkStream stream = client.GetStream();
            StreamReader reader = new StreamReader(stream);

            // Enter the listening loop.
            while (true)
            {
                int i;

                // Loop to receive all the data sent by the client. 
                if ((data = reader.ReadLine()) != null)
                {
                    Thread thread = new Thread(() => ThreadProc(data));
                    thread.Start();
                }
            }

        }
        catch (SocketException err)
        {
            Console.WriteLine("SocketException: {0}", err);
        }
        finally
        {
            // Stop listening for new clients.
            client.Close();
        }
    }
}

由于某种原因,this.Invoke((MethodInvoker)委托()范围内的所有代码都没有发生。现在和Google一起坐了几天......谁能告诉我为什么以及怎么样解决它?

非常感谢

0 个答案:

没有答案