The lock keyword s a statement block as a critical section.lock(expression) statement_block
where: expression
Specifies the object that you want to lock on. expression must be a reference type.
Typically, expression will either be this, if you want to protect an instance variable, or typeof(class), if you want to protect a static variable (or if the critical section occurs in a static method in the given class). statement_block
The statements of the critical section.
Res
lock ensures that one thread does not enter a critical section while another thread is in the critical section of code.//from Msdn
where: expression
Specifies the object that you want to lock on. expression must be a reference type.
Typically, expression will either be this, if you want to protect an instance variable, or typeof(class), if you want to protect a static variable (or if the critical section occurs in a static method in the given class). statement_block
The statements of the critical section.
Res
lock ensures that one thread does not enter a critical section while another thread is in the critical section of code.//from Msdn
解决方案 »
- webservicr引用多个类
- 想学习下wpf,谁有wpf初学的教材或是视频或是下载的网址呢
- 对checklistbox控件无语了!昏,它里面ITEMS怎么不区分TEXT和VALUE
- 请教下WinForm中窗体间传值问题?
- 学习c#将近两个月了啊,感觉水平还是非常菜的那种,大家推荐几本有关算法的书籍。
- 求一句SQL
- 如何在visual studio 2005中,向sql server数据库中的image数据类型添加图片
- C#集合的问题
- 如何使用 Regulator 工具啊。 最好说详细点
- 大家请你帮帮忙, 我想实现动态控建名
- 怎么写被继承的基类form中的控件的事件?
- 求在C#下进行windows ad 编程的教程和实例!在线等待结帖
lock ( expression ) embedded-statement
The expression of a lock statement must denote a value of a reference-type. An implicit boxing conversion (Section 6.1.5) is never performed for the expression of a lock statement, and thus it is a compile-time error for the expression to denote a value of a value-type.A lock statement of the formlock (x) ...
where x is an expression of a reference-type, is precisely equivalent toSystem.Threading.Monitor.Enter(x);
try {
...
}
finally {
System.Threading.Monitor.Exit(x);
}
except that x is only evaluated once.The System.Type object of a class can conveniently be used as the mutual-exclusion lock for static methods of the class. For example:class Cache
{
public static void Add(object x) {
lock (typeof(Cache)) {
...
}
}
public static void Remove(object x) {
lock (typeof(Cache)) {
...
}
}
}
此处: expression
指定要锁定的对象。expression 必须是引用类型。
通常,如果要保护实例变量,则 expression 为 this;如果要保护 static 变量(或者如果临界区出现在给定类的静态方法中),则 expression 为 typeOf (class)。 statement_block
临界区的语句。
我在程序中有个Webservers的类category其中有个用户确认的方法checkuser(),和数据初始化的方法DataSynchro()。
我想用一个progressBar来显示确认和数据初始化的进度。该如何做?
明白了,自然就知道lock是干嘛了。