2

我有以下带有嵌套私有类的类,我想将 to 的值分配NIDContext。我使用过 getter 和 setter,但是NID( Sequencer.Context = value;) 的值永远不会分配给 ( SeqNode = Node.LoadNode(Context);)。我究竟做错了什么?

//Instantiation
Increment.NID = "/Root/Dir/NodetoIncrement";
String getSequence = Convert.ToString(Increment.SID);

// The Class
public static class Increment
{
    //Value of the node Context location in the tree to increment ( Increment.NID )
    public static string NID
    {
        set { Sequencer.Context = value; } //The "/Root/Dir/NodetoIncrement";
    }

    //Get the sequence ID
    public static int SID
    {
        get { return Sequencer.GetSeqId; }
    }

    //Nested sequencer class. This increments the node.

    private class Sequencer
    {   
        private Node SeqNode;
        private static int SequenceNumber;
        private volatile bool Run = true;

        public static string Context { get; set; } //gets 

        public static int GetSeqId
        {
            get
            {
                return Interlocked.Add(ref SequenceNumber, 1);
            }
        }

        public Sequencer() //Constructor Here!
        {
            SeqNode = Node.LoadNode(Context);
            SequenceNumber = Convert.ToInt16(SeqNode["LastSequenceNo"]);

            //NEVER DO THIS .. causes the system to lockup see comments.
            System.Threading.Tasks.Task.Factory.StartNew(() =>
            {
                while (Run)
                {
                    Save();
                    Thread.Sleep(5000);
                }
            });
        }

        private void Save()
        {
            //optimistic concurrency recommended!!
            var retryMaxCount = 3;             // maximum number of attempts
            var cycles = 0;                    // current attempt
            Exception exception = null;        // inner exception storage
            while (cycles++ < retryMaxCount)   // cycle control
            {
                try
                {
                    SeqNode["LastSequenceNo"] = Convert.ToString(SequenceNumber);
                    SeqNode.Save();

                    // successful save, exit loop
                    exception = null;
                    break;
                }
                catch (NodeIsOutOfDateException e)
                {
                    exception = e; // storing the exception temporarily
                    SeqNode = Node.LoadNode(Context);
                }
            }
            // rethrow if needed
            if (exception != null)
                throw new ApplicationException("Node is out of date after 3 attempts.", exception);
        }


        ~Sequencer() { Save(); }

    }
}

public class XYHandler : Node
{
    public override void Save(NodeSaveSettings settings)
    {
        base.Name = Convert.ToString(Increment.SID);
        base.Save();
    }

    public override bool IsContentType
    {
        get { return true; }
    }
}
4

3 回答 3

7

我究竟做错了什么?

您正在等待静态初始化程序中的另一个线程。 永远不要那样做。我不能足够强烈地强调这是多么疯狂的危险。

有关原因的解释,请参阅此答案:

https://stackoverflow.com/a/8883117/88656

于 2013-10-12T01:53:56.357 回答
0

这里有一些时间耦合吗?

SeqNode在实例化时设置Sequencer,但我在您的示例中看不到实例化。

静态构造函数将在第一次调用属性设置器之前运行,然后在 5 秒后重试 - 何时设置属性?

于 2013-10-12T00:12:24.943 回答
0

我看不到 Sequencer 的构建位置(也许我错过了它)。由于它不是静态构造函数,因此必须至少调用一次 LoadNode 才能运行。您是否也打算使该构造函数成为静态的?

于 2013-10-12T00:16:10.717 回答