多线程程序的不确定性

ContractedBlock.gifExpandedBlockStart.gif代码
  1None.gifusing System;
  2None.gifusing System.Collections;
  3None.gifusing System.Collections.Generic;
  4None.gifusing System.ComponentModel;
  5None.gifusing System.Data;
  6None.gifusing System.Drawing;
  7None.gifusing System.Text;
  8None.gifusing System.Windows.Forms;
  9None.gif
 10None.gifusing System.Threading;
 11None.gif
 12None.gifnamespace CSharpGeriac
 13ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 14InBlock.gif    public partial class Form1 : Form
 15ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 16InBlock.gif        public Form1()
 17ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 18InBlock.gif            InitializeComponent();
 19InBlock.gif
 20InBlock.gif            Thread t = new Thread(new ThreadStart(ChangeLabel));
 21InBlock.gif            t.Start();
 22ExpandedSubBlockEnd.gif        }

 23InBlock.gif
 24InBlock.gif        private void ChangeLabel()
 25ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 26InBlock.gif            myClassCollection coll = new myClassCollection();
 27InBlock.gif            foreach (MyClass c in coll)
 28ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 29InBlock.gif                SetLabelText(c);
 30InBlock.gif                Thread.Sleep(1000);
 31ExpandedSubBlockEnd.gif            }

 32ExpandedSubBlockEnd.gif        }

 33InBlock.gif
 34InBlock.gif        private void SetLabelText(MyClass myClass)
 35ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 36InBlock.gif            // label.Text = number.ToString();
 37InBlock.gif            // Do NOT do this, as we are on a different thread.
 38InBlock.gif
 39InBlock.gif            // Check if we need to call BeginInvoke.
 40InBlock.gif            if (this.InvokeRequired)
 41ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 42InBlock.gif                // Pass the same function to BeginInvoke,
 43InBlock.gif                // but the call would come on the correct
 44InBlock.gif                // thread and InvokeRequired will be false.
 45InBlock.gif                this.BeginInvoke(new SetLabelTextDelegate(SetLabelText),
 46ExpandedSubBlockStart.gifContractedSubBlock.gif                                                 new object[] dot.gif{ myClass });
 47InBlock.gif
 48InBlock.gif                return;
 49ExpandedSubBlockEnd.gif            }

 50InBlock.gif
 51InBlock.gif            label1.Text = myClass.Name;
 52ExpandedSubBlockEnd.gif        }

 53InBlock.gif
 54InBlock.gif        private delegate void SetLabelTextDelegate(MyClass myClass);
 55ExpandedSubBlockEnd.gif    }

 56InBlock.gif
 57InBlock.gif    public class myClassCollection
 58ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 59ContractedSubBlock.gifExpandedSubBlockStart.gif        [Filed]#region [Filed]
 60InBlock.gif
 61InBlock.gif        private IList<MyClass> myClassItems = new List<MyClass>();
 62InBlock.gif
 63ExpandedSubBlockEnd.gif        #endregion

 64InBlock.gif
 65ContractedSubBlock.gifExpandedSubBlockStart.gif        [Constructor]#region [Constructor]
 66InBlock.gif
 67InBlock.gif        public myClassCollection()
 68ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 69InBlock.gif            for (int i = 0; i < 100; i++)
 70ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 71InBlock.gif                MyClass my = new MyClass();
 72InBlock.gif                my.Name = "Nmae" + new Random().Next(100).ToString();
 73InBlock.gif                my.Age = new Random().Next(100);
 74InBlock.gif                myClassItems.Add(my);
 75InBlock.gif                Thread.Sleep(10);//加了这句和不加这句,结果不一样。
 76ExpandedSubBlockEnd.gif            }

 77ExpandedSubBlockEnd.gif        }

 78InBlock.gif
 79ExpandedSubBlockEnd.gif        #endregion

 80InBlock.gif
 81ContractedSubBlock.gifExpandedSubBlockStart.gif        [property]#region [property]
 82InBlock.gif
 83InBlock.gif        public IList<MyClass> MyCollection
 84ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 85InBlock.gif            get
 86ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 87InBlock.gif                return myClassItems;
 88ExpandedSubBlockEnd.gif            }

 89InBlock.gif            set
 90ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 91InBlock.gif                myClassItems = value;
 92ExpandedSubBlockEnd.gif            }

 93ExpandedSubBlockEnd.gif        }

 94InBlock.gif
 95ExpandedSubBlockEnd.gif        #endregion

 96InBlock.gif
 97ContractedSubBlock.gifExpandedSubBlockStart.gif        [index]#region [index]
 98InBlock.gif
 99InBlock.gif        public MyClass this[int index]
100ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
101InBlock.gif            get
102ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
103InBlock.gif                return (MyClass)myClassItems[index];
104ExpandedSubBlockEnd.gif            }

105ExpandedSubBlockEnd.gif        }

106InBlock.gif
107ExpandedSubBlockEnd.gif        #endregion

108InBlock.gif
109ContractedSubBlock.gifExpandedSubBlockStart.gif        GetEnumerator#region GetEnumerator
110InBlock.gif
111InBlock.gif        public IEnumerator GetEnumerator()
112ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
113InBlock.gif            foreach (MyClass c in myClassItems)
114ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
115InBlock.gif                yield return c;
116ExpandedSubBlockEnd.gif            }

117ExpandedSubBlockEnd.gif        }

118InBlock.gif
119ExpandedSubBlockEnd.gif        #endregion

120InBlock.gif
121ExpandedSubBlockEnd.gif    }

122InBlock.gif
123InBlock.gif    public class MyClass
124ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
125ContractedSubBlock.gifExpandedSubBlockStart.gif        [Constructor]#region [Constructor]
126ExpandedSubBlockEnd.gif        #endregion

127InBlock.gif
128ContractedSubBlock.gifExpandedSubBlockStart.gif        [Filed]#region [Filed]
129InBlock.gif
130InBlock.gif        private string _name;
131InBlock.gif
132InBlock.gif        private int _age;
133InBlock.gif
134ExpandedSubBlockEnd.gif        #endregion

135InBlock.gif
136ContractedSubBlock.gifExpandedSubBlockStart.gif        [property]#region [property]
137InBlock.gif
138InBlock.gif        public string Name
139ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
140InBlock.gif            get
141ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
142InBlock.gif                return _name;
143ExpandedSubBlockEnd.gif            }

144InBlock.gif            set
145ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
146InBlock.gif                _name = value;
147ExpandedSubBlockEnd.gif            }

148ExpandedSubBlockEnd.gif        }

149InBlock.gif
150InBlock.gif        public int Age
151ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
152InBlock.gif            get
153ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
154InBlock.gif                return _age;
155ExpandedSubBlockEnd.gif            }

156InBlock.gif            set
157ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
158InBlock.gif                _age = value;
159ExpandedSubBlockEnd.gif            }

160ExpandedSubBlockEnd.gif        }

161InBlock.gif
162ExpandedSubBlockEnd.gif        #endregion

163ExpandedSubBlockEnd.gif    }

164InBlock.gif
165ExpandedBlockEnd.gif}
/Files/nanshouyong326/CSharpGeriac.rar

程序中我加入注释的一句:Thread.Sleep(10);//加了这句和不加这句,结果不一样。高手可以测试下。我不明白为什么,,请高手指点!!!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值