Problems with access to modified closure closures

Source: Internet
Author: User
Tags closure

  for(inti =0; i < ListBoxDevices.Items.Count; i++)                {                    varTempdeviceid =Listboxdevices.items[i].                    ToString (); if(Tempdeviceid.contains ("(drop line)"))                    {                        varid = Globalconvert.getdeviceid (tempdeviceid.replace ("(drop line)",string.                        Empty)); if(id = =Reonlineid) {Listboxdevices.invoke (MethodInvoker)Delegate                            {                                Try{Listboxdevices.items[i]=Globalconvert.getdeviceid (Reonlineid); }                                Catch(Exception ex) {ExceptionLog.Instance.WriteLog (ex,                                LOGTYPE.UI);                        }                            }); }                    }                }

There's a for loop inside the code.

An anonymous delegate is called Inside the loop

Simulate a bit:

But the execution of the following code does not appear to be the problem is normal

  Private voidButton1_Click (Objectsender, EventArgs e) {Thread Thread=NewThread (Closure); Thread. IsBackground=true; Thread.        Start (); }        Private voidClosure () { for(inti =0; I <Ten; i++)            {                 This. Invoke ((MethodInvoker)Delegate{Textbox1.appendtext (string. Format ("{0}:{1}{2}", DateTime.Now.ToString ("YYYY-MM-DD HH:mm:ss.fff") , I, Environment.NewLine));            }); }        }



A closure in C # takes the form of an in-line delegate/anonymous method.

A closure is attached to its parent method meaning this variables defined in parent's method body can be referenced from W Ithin the anonymous method.


Problems with access to modified closure closures

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: 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.