I have a code that just dies on its task(interface for 10 minutes) when processing + - 3500 lines of data.I got the idea to make asynchronous execution of this work.The problem is that I didn’t really understand how to use asynchrony in such examples like mine!
C #code:
async void Button7Click(object sender, EventArgs e)
 {
 try
 {
 textBox1.Text="1";
 dataGridView2.SelectAll();
dataGridView2.ClearSelection();
for(int i=0;i<dataGridView1.RowCount - 1;i ++)
{
 if(this.dataGridView2.CurrentRow!=null)
{

dataGridView2.Rows.Add();
this.dataGridView2.Rows[i] .Cells[0] .Value=Convert.ToDouble(((Convert.ToDouble(dataGridView1[2, i] .Value) - Convert.ToDouble(dataGridView1[2, 0] .Value)) * Convert.ToDouble(textBox1.Text));

this.dataGridView2.Rows[i] .Cells[1] .Value +=string.Format("{0: 0.#############}", Convert.ToDouble((Convert.ToDouble(dataGridView1[7, i] .Value) - Convert.ToDouble(dataGridView1[7, 0] .Value))/78));

this.dataGridView2.Rows[i] .Cells[2] .Value +=string.Format("{0: 0.#########"", Convert.ToDouble((Convert.ToDouble(dataGridView1[12 , i] .Value) - Convert.ToDouble(dataGridView1[12, 0] .Value))/Convert.ToDouble((Convert.ToDouble(dataGridView1[15, 0] .Value) - Convert.ToDouble(dataGridView1[12, 0 ] .Value))))) + Environment.NewLine;

 this.dataGridView2.Rows[i] .Cells[3] .Value +=string.Format("{0: 0.###}", Convert.ToDouble((Convert.ToDouble(dataGridView1[31, i] .Value )/3) + Convert.ToDouble(((Convert.ToDouble(dataGridView1[15, i] .Value) - Convert.ToDouble(dataGridView1[12, i] .Value))))) + Environment.NewLine;

this.dataGridView2.Rows[i] .Cells[4] .Value=Convert.ToDouble(((Convert.ToDouble(dataGridView1[31, i] .Value)/2)) + Environment.NewLine;

this.dataGridView2.Rows[i] .Cells[5] .Value +=string.Format("{0: 0.#####}", Convert.ToDouble((Convert.ToDouble(dataGridView1[31, i] .Value)/3) + Convert.ToDouble((Convert.ToDouble(dataGridView1[15, i] .Value) - Convert.ToDouble(dataGridView1[12, i] .Value)) * 3))) + Environment.NewLine;
}
else
 {
i--;
dataGridView2.Rows.RemoveAt(i);
GC.Collect();
GC.WaitForPendingFinalizers();
return;
}
}
 }
catch(Exception e2)
{
//Handle Exception - write to the log or output e.Message to the panel
MessageBox.Show(e2.Message);
}
}

Datagridview1 stores numbers(double), which are used in calculations
In datagridview2 the answers that were calculated

spoiler
It would be perfect if someone helped me with this code + correctly set collect and other details that clear garbage, because after such a calculation I am packed with a PC cache of almost 1000mb

But if there is another way to speed up this process, then write.

3 Answers 3

In your case, the view is constantly updated, and this is the UI part.That is, it cannot be transferred to a separate thread.
What you should do here:
yourDataGridView.SuspendDrawing();
//update your fields
yourDataGridView.ResumeDrawing();

And minor improvements:
var row=this.dataGridView2.Rows[i];//to pull less checks on the index
row.Cells[0] .Value=...;

It would be possible to construct string.Format("{0: 0.#############}"......to put a separate stream and enter the already prepared data into the GridView
  • hmm...
    no suspendDrawning and ResumeDrawing(), there is only suspendlayout and resumelayout in
    where

    Regarding the minor improvement, nothing has changed(the program runs in the same time as before, but the code has become more readable)
    – Incomprehensible72 Jul 18 '19 at 14:14
1 - advice - do not jerk the garbage collector on trifles.In this way, you are guaranteed to worsen the execution time.At the same time, he himself will cope superbly while you consider the result

2 - master binding to external objects.Windows Forms DataGridView is able to https://docs.microsoft.com/ru-ru/dotnet/framework/...

in the simplest case, you can practice on kitties try arrays/lists( and it is possible to tighten the Entity Framework, there is a database option"in memory".but only if there is data loaded from external databases, or stored in a database ..although EF is already all in async/await"out of the box")

3 - binding from bavit you from handwriting create/delete rows of DataGridView.These are heavy operations, considering that we are talking about visual elements

short summary - binding to arrays/lists/DB, and their processing in external processes, or PLINQ..or async/await - that you can master
You do not need asynchrony;you need to move a heavy operation to a separate thread.
You don't need to work with the UI here, keep your data in a separate data structure.You can use a two-dimensional array or DataTableOver it and perform calculations, upon completion of processing, update the UI.

The easiest way is to use BackgroundWorker, example -controls" rel="nofollow">here.
Well, it is not clear why 3.5k lines simultaneously display on the form?
  • In my opinion, BackgroundWorker is a bit old – Unusual26 Jul 18 '19 at 23:09
  • Because what needs to be checked after the calculations and so that there is an opportunity to change the value in the cell, if necessary – Incomprehensible72 Jul 19 '19 at 10:37
  • [[ElemAnybody]], I would advise you to rewrite the project on WPF - there bindings work better.

    The current version is incorrect literally from the beginning to the end.Calculations are conducted separately, the display is separate.

    I can help with the code, but I need to know more details, for example, the need for selections is completely unclear from the code.
    – Unusual26 Jul 19 '19 at 13:36
  • [[ElemAnybody]], Well, this is not a reason to simultaneously display 3.5k lines at once.You are not going to see all the lines at the same time? – Splendid Sloth Jul 19 '19 at 19:18