C# 在C中锁定静态方法#
我想在C#中锁定一个静态方法,这是java中同步的替代方法 方法如下C# 在C中锁定静态方法#,c#,java,.net,locking,C#,Java,.net,Locking,我想在C#中锁定一个静态方法,这是java中同步的替代方法 方法如下 public synchronized static void UpdateDialListFile(String filePath, int lineno, String lineToBeInserted) 现在我在C#中实现这个函数,同步的替代方法在C中是如何工作的# 正在从线程内调用上述方法,如下所示: new DialList(oDialInfo.FileName,oDialInfo.AppId,
public synchronized static void UpdateDialListFile(String filePath, int lineno,
String lineToBeInserted)
现在我在C#中实现这个函数,同步的替代方法在C中是如何工作的#
正在从线程内调用上述方法,如下所示:
new DialList(oDialInfo.FileName,oDialInfo.AppId,oDialInfo.AppCode,
pausetime,oDialInfo, PropVal).start();
[MethodImpl(methodimpoptions.Synchronized)]
如果要同步整个方法,可以使用此选项
类似问题:
[MethodImpl(methodimpoptions.Synchronized)]
如果要同步整个方法,可以使用此选项
类似问题:
您可以像这样使用来序列化对关键部分或资源的访问。guard对象的范围/含义完全由您决定:
public class Widget
{
// scope of 'latch'/what it represents is up to you.
private static readonly object latch = new object() ;
public void DoSomething()
{
DoSomethingReentrant() ;
lock ( latch )
{
// nobody else may enter this critical section
// (or any other critical section guarded by 'latch'
// until you exit the body of the 'lock' statement.
SerializedAccessDependentUponLatch() ;
}
DoSomethingElseReentrant() ;
return ;
}
}
CLR中可用的其他同步原语列在中。您可以使用类似的方法来序列化对关键部分或资源的访问。guard对象的范围/含义完全由您决定:
public class Widget
{
// scope of 'latch'/what it represents is up to you.
private static readonly object latch = new object() ;
public void DoSomething()
{
DoSomethingReentrant() ;
lock ( latch )
{
// nobody else may enter this critical section
// (or any other critical section guarded by 'latch'
// until you exit the body of the 'lock' statement.
SerializedAccessDependentUponLatch() ;
}
DoSomethingElseReentrant() ;
return ;
}
}
CLR中可用的其他同步原语列在中。您也可以使用Monitor.Enter和Monitor.Exit来锁定关键部分。您也可以使用Monitor.Enter和Monitor.Exit来锁定关键部分。谢谢,如果我想同步整个方法,您已经提到了。如果我想锁定方法的一部分,还有什么其他可能的方法呢。有很多方法。这取决于你需要什么
lock
是一个可以开始使用的方法。谢谢,您提到了如果我想同步整个方法。如果我想锁定方法的一部分,还有什么其他可能的方法呢。有很多方法。这取决于你需要什么<代码>锁定是一个可以开始的选项。