Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C中锁定静态方法#_C#_Java_.net_Locking - Fatal编程技术网

C# 在C中锁定静态方法#

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,

我想在C#中锁定一个静态方法,这是java中同步的替代方法

方法如下

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
是一个可以开始使用的方法。谢谢,您提到了如果我想同步整个方法。如果我想锁定方法的一部分,还有什么其他可能的方法呢。有很多方法。这取决于你需要什么<代码>锁定是一个可以开始的选项。