ControlCollection.IsSynchronized 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取一个值,该值指示是否同步 ControlCollection 对象。
public:
property bool IsSynchronized { bool get(); };
public bool IsSynchronized { get; }
member this.IsSynchronized : bool
Public ReadOnly Property IsSynchronized As Boolean
属性值
此属性始终为 false
。
实现
示例
下面的代码示例创建一个方法,该方法通过 ControlCollection 控件 myButton
的 Button 集合枚举。 创建枚举器时, IsSynchronized 将检查 属性以查看操作是否是线程安全的;如果不是,则 SyncRoot 使用 属性获取 对象以使操作线程安全。 枚举完成后,属性的值 IsReadOnly 将作为 Text 包含页上控件 Label 的 属性写入。
// Create a method that enuberates through a
// button//s ControlCollection in a thread-safe manner.
public void ListControlCollection(object sender, EventArgs e)
{
IEnumerator myEnumerator = myButton.Controls.GetEnumerator();
// Check the IsSynchronized property. If False,
// use the SyncRoot method to get an object that
// allows the enumeration of all controls to be
// thread safe.
if (myButton.Controls.IsSynchronized == false)
{
lock (myButton.Controls.SyncRoot)
{
while (myEnumerator.MoveNext())
{
Object myObject = myEnumerator.Current;
LiteralControl childControl = (LiteralControl)myEnumerator.Current;
Response.Write("<b><br /> This is the text of the child Control </b>: " +
childControl.Text);
}
msgReadOnly.Text = myButton.Controls.IsReadOnly.ToString();
}
}
}
' Create a method that enuberates through a
' button's ControlCollection in a thread-safe manner.
Public Sub ListControlCollection(sender As Object, e As EventArgs)
Dim myEnumerator As IEnumerator = myButton.Controls.GetEnumerator()
' Check the IsSynchronized property. If False,
' use the SyncRoot method to get an object that
' allows the enumeration of all controls to be
' thread safe.
If myButton.Controls.IsSynchronized = False Then
SyncLock myButton.Controls.SyncRoot
While (myEnumerator.MoveNext())
Dim myObject As Object = myEnumerator.Current
Dim childControl As LiteralControl = CType(myEnumerator.Current, LiteralControl)
Response.Write("<b><br /> This is the text of the child Control </b>: " & _
childControl.Text)
End While
msgReadOnly.Text = myButton.Controls.IsReadOnly.ToString()
End SyncLock
End If
End Sub