简体   繁体   中英

Adding data read from the socket to DataGridView (in c#)

I created a socket listener using C#. I want to list the data read on this socket instantly in the DataGridWiew. (about 100 data per second is transferred through this socket) I successfully read the data on the socket, but while trying to list it on the DataGridView instantly, the screen freezes until the data flow stops. When data flow stops, all data are listed on the DataGridView. But instead I want the DataGridView to be successfully refreshed continuously as data is added.

Is there a way to list so much data successfully in DataGridView instantly?

Where am I making mistakes?

The code:

        frm.loggGridView.ColumnCount = 5;
        frm.loggGridView.ColumnHeadersVisible = true;
        DataGridViewCellStyle columnHeaderStyle = new DataGridViewCellStyle();
        columnHeaderStyle.BackColor = Color.Beige;
        columnHeaderStyle.Font = new Font("Verdana", 10, FontStyle.Bold);
        frm.loggGridView.ColumnHeadersDefaultCellStyle = columnHeaderStyle;

        frm.loggGridView.Columns[0].Name = "ID";
        frm.loggGridView.Columns[1].Name = "LATITUDE";
        frm.loggGridView.Columns[2].Name = "LONGITUDE";
        frm.loggGridView.Columns[3].Name = "ALTITUDE";
        frm.loggGridView.Columns[4].Name = "TIME";


        int i= 0;
        try
        {
            PointF p1;
            while (true)
            {

                byte[] bytes = listener.Receive(ref groupEP);
                String FlightData = Encoding.UTF8.GetString(bytes, 0, bytes.Length);

                //  Console.WriteLine("broadcast veri {0} :\n {1}\n", groupEP.ToString(), FlightData);

                String[] FlightDataSplit = FlightData.Split(' ', '\n');
                p1 = new PointF(FlightDataSplit[1], FlightDataSplit[0], FlightDataSplit[2]);

                frm.loggGridView.Rows.Add();

                frm.loggGridView.Rows[i].Cells[0].Value = i;

                frm.loggGridView.Rows[i].Cells[1].Value = p1.latitude;

                frm.loggGridView.Rows[i].Cells[2].Value = p1.longitude;

                frm.loggGridView.Rows[i].Cells[3].Value = p1.altitude;

                frm.loggGridView.Rows[i].Cells[4].Value = DateTime.Now;

               i++;


            }


        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }

I tried to mock up a quick LINQPad example. This will continue to add new results in the background, without the application freezing


// Simple GridView control
DataGridView gridView = new System.Windows.Forms.DataGridView();

gridView.Columns.Add("A", "A");
gridView.Columns.Add("B", "B");
gridView.Columns.Add("C", "C");

// Display the Control in LINQPad
PanelManager.DisplayControl(gridView);

await Task.Run(async () => 
{
    while(true)
    {
        // your listener code       
        var results = Enumerable.Range(0, 100).Select(x => x).ToList();

        if(gridView.IsHandleCreated)
        {
            // render on the UI thread
            gridView.Invoke((MethodInvoker)delegate
            {
                for (int i = 0; i < results.Count; i++)
                {
                    int index = gridView.Rows.Add();
                    gridView.Rows[index].Cells[0].Value = 1;
                    gridView.Rows[index].Cells[1].Value = 2;
                    gridView.Rows[index].Cells[2].Value = 3;
                }
            });
        }

        await Task.Delay(1000); // remove this from your solution, your listener is blocking
    }

});




The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM