Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.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#_.net_Multithreading_C# 4.0 - Fatal编程技术网

C# 异常:从未同步的代码块调用了对象同步方法

C# 异常:从未同步的代码块调用了对象同步方法,c#,.net,multithreading,c#-4.0,C#,.net,Multithreading,C# 4.0,我有几个线程写入相同的int。每个线程增加 整数值。同步增量操作的简单方法是什么。 lock语句仅对对象有效,因此我无法使用它。我也试过了 以下: static int number=0; static void Main(string[] args) { ThreadStart ts = new ThreadStart(strtThread); new Thread(ts).Start(); new Thread(ts).Start()

我有几个线程写入相同的int。每个线程增加 整数值。同步增量操作的简单方法是什么。 lock语句仅对对象有效,因此我无法使用它。我也试过了 以下:

static int number=0;

static void Main(string[] args)
    {
        ThreadStart ts = new ThreadStart(strtThread);
        new Thread(ts).Start();
        new Thread(ts).Start();
        new Thread(ts).Start();
        new Thread(ts).Start();
        new Thread(ts).Start();
        new Thread(ts).Start();
        Console.ReadLine();
    }

    public static void strtThread()
    {
        bool lockTaken = false;

        Monitor.Enter(number,ref lockTaken);
        try
        {
            Random rd = new Random();
            int ee = rd.Next(1000);
            Console.WriteLine(ee);
            Thread.Sleep(ee);
            number++;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        finally 
        {
            if (lockTaken)
            {
             Monitor.Exit(number);
            }

        }
    }
它给了我以下错误:

从未同步的代码块调用了对象同步方法

可以使用自动递增整数而不锁定:

public static void strtThread()
{
    Interlocked.Increment(ref number);
}
如果有多条语句,则可以创建一个实例,该实例可以:

可以使用自动递增整数而不锁定:

public static void strtThread()
{
    Interlocked.Increment(ref number);
}
如果有多条语句,则可以创建一个实例,该实例可以:


你为什么要费心处理
号码
?我想这就是问题所在。试试这个:

static Object locking = new Object();

static void Main(string[] args)
    {
        ThreadStart ts = new ThreadStart(strtThread);
        new Thread(ts).Start();
        new Thread(ts).Start();
        new Thread(ts).Start();
        new Thread(ts).Start();
        new Thread(ts).Start();
        new Thread(ts).Start();
        Console.ReadLine();
    }

    public static void strtThread()
    {
        lock(locking) {
            try
            {
                Random rd = new Random();
                int ee = rd.Next(1000);
                Console.WriteLine(ee);
                Thread.Sleep(ee);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }

你为什么要费心处理
号码
?我想这就是问题所在。试试这个:

static Object locking = new Object();

static void Main(string[] args)
    {
        ThreadStart ts = new ThreadStart(strtThread);
        new Thread(ts).Start();
        new Thread(ts).Start();
        new Thread(ts).Start();
        new Thread(ts).Start();
        new Thread(ts).Start();
        new Thread(ts).Start();
        Console.ReadLine();
    }

    public static void strtThread()
    {
        lock(locking) {
            try
            {
                Random rd = new Random();
                int ee = rd.Next(1000);
                Console.WriteLine(ee);
                Thread.Sleep(ee);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }

提供原子增量。通常“为多个线程共享的变量提供原子操作。”

提供原子增量。通常“为多个线程共享的变量提供原子操作。”

我想使用Monitor.Enter来实现这一点。请给我一个使用Monitor.Enter的代码示例。谢谢您的帮助。@Vero009:为什么需要使用监视器。输入?不使用
lock
是非常不寻常的。我想使用Monitor.Enter来执行此操作。请给我一个使用Monitor.Enter的代码示例。谢谢您的帮助。@Vero009:为什么需要使用监视器。输入?不使用
锁是非常不寻常的。