English 中文(简体)
自动重新设置 Event 和 Soneton 问题
原标题:AutoresetEvent and Singleton issue

有人能告诉我下面的代码有什么问题吗? 理想的情况是, 它应该先开始一个线索, 然后等待设定的事件。 它不会启动线索, 然后被困在 WaitOne () 上 。

我想知道线是怎么回事 以及为什么?

class Program
{   
    static void Main(string[] args)
    {
        Testing t = Testing.Instance;
        Console.Read();
    }
}


class Testing
{
    private static AutoResetEvent evt = new AutoResetEvent(false);
    public static Testing Instance = new Testing();

    private Testing()
    {
        Create();
        evt.WaitOne();
        Console.WriteLine("out");
    }

    private void Create()
    {
        Console.WriteLine("Starting thread");
        new Thread(Print).Start();
    }

    private void Print()
    {
        Console.WriteLine("started");
        evt.Set();
    }
}

EDIT: So far, the description provided by @BrokenGlass makes sense. but changing the code to the following code allows another thread can access the instance methods without constructor being completed.(Suggested by @NicoSchertler).

private static Testing _Instance;

public static Testing Instance
{
get
{
    if (_Instance == null)
        _Instance = new Testing();
    return _Instance;
}
}
最佳回答

我怀疑这种行为的根本原因是,在构造器完成执行之前,产卵线无法进入 Print 方法,但构造器无法完成执行,因为它正在等待仅由Print 方法触发的信号。

evt.waitOne () 替换为长的 Thread.Slip () 呼叫确认同一行为 - 构造器必须完成运行, 才能从其它线索执行对象的任何实例方法 。

问题回答

问题是第二串线索创建得太早。 我不知道为什么, 但当主程序启动前启动时, 它将不会执行 。

您应该使用原版本中的单吨模式。 这将有效 。

private static Testing _Instance;

public static Testing Instance
{
    get
    {
        if (_Instance == null)
            _Instance = new Testing();
        return _Instance;
    }
}

此外,您不应该将evt变量固定在静态中。在多数情况下,例变量应该是单吨级中唯一的静态成员。

我的猜测将会与静态字段初始化的相对时间发生问题。 尝试在 < code > 测试 < / code > 构建器中初始化 < code> < evt , 代之以 :

private static AutoResetEvent evt;
public static Testing Instance = new Testing();

private Testing()
{
    evt = new AutoResetEvent(false);
    Create();
    evt.WaitOne();
    Console.WriteLine("out");
}

我该注意到这只是个猜测 我想这个代码能行得通





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签