Because the write program involves multi-thread maintenance of a dview, And then it often gets stuck when it finds it painful... At first, I thought it was the cause of the read/write conflict. Then I applied the lock and the problem remained... Then the program will not respond every time the scroll bar appears, so it should feel like a problem with the scroll bar...
I can solve the problem by using Invoke on the Internet. After a try, it may be because the method I used is incorrect or the problem is not solved.-_-|
Finally solved with InvokeRequired...
Because the code for modifying the DataGridView is written in the form, so...
private static object obj = new object(); private delegate void ChangeFunction(PackageMessage pm, string Message); public void AddWarning(PackageMessage pm, string Message) { if (dgv.InvokeRequired) { ChangeFunction c = new ChangeFunction(AddWarning); this.Invoke(c, new object[] { pm, Message }); } else { lock (obj) { DataRow dr = MessageDT.NewRow(); dr[0] = pm.GetSendTime().ToString(); dr[1] = pm.GetSourceIP().ToString() + ":" + pm.GetSourcePort().ToString(); dr[2] = pm.GetDestinaionIP().ToString() + ":" + pm.GetDestinationPort().ToString(); dr[3] = Message; MessageDT.Rows.Add(dr); } } }
It indicates that dgv is a DataGridView object. At first, it was better to bind a DataTable dt, and the result was useless... -_-|
Just add InvokeRequired. It seems that there is no problem when the lock is not added, but I added it for security reasons...
Write functional code in else, or in lock (if you lock it). The form of if is similar, that is, the parameters should be different ~