C # solution for program freezing (no response) caused by the scroll bar of the multi-thread write dview

Source: Internet
Author: User

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 ~


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.