相关疑难解决方法(0)

静态初始化程序中的Task.Run

请考虑以下代码.

static class X
{
    public static int Value = Task.Run(() => 0).Result;
}

class Program
{
    static void Main(string[] args)
    {
        var value = X.Value;
    }
}
Run Code Online (Sandbox Code Playgroud)

调用Task.Run然后Result在静态初始化程序中导致程序永久冻结.为什么?

.net c# task-parallel-library .net-4.5

13
推荐指数
1
解决办法
245
查看次数

具有静态类的竞争条件?

假设我有一个带静态方法的静态类.

多个线程可以同时调用此静态方法.

在这些情况下是否存在竞争条件:

a - if the method depends only on local variables
b - if the method depends on local variables and member fields
Run Code Online (Sandbox Code Playgroud)

c# static multithreading

5
推荐指数
2
解决办法
3354
查看次数