2013-10-12 31 views
2

我有以下類嵌套的私人類,我想分配NID的值Context。我已經使用了getter和setter,但是NIDSequencer.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; } 
    } 
} 
+0

沒有製造領域的民營任何一點,類已經是私有的。這只是公開並解決你的問題。 –

+0

我認爲你沒有提供足夠的信息來幫助我們。 –

+0

我也添加了實例化。 – Xdrone

回答

0

這裏有一些暫時的耦合嗎?

SeqNode在實例化Sequencer時設置,但我看不到示例中的實例。

靜態構造函數將在第一次調用屬性setter之前運行,然後在5s後再次嘗試 - 什麼時候屬性被設置?

+0

是的,暫時的聯結在這裏!在Sequencer可以遞增之前,遞增節點被傳遞給SeqNode。 「靜態構造函數將在第一次調用屬性設置器之前運行,然後在5秒後再次嘗試 - 何時設置屬性?」沒有構造函數(SeqNode = Node.LoadNode(Context);)應該觸發應該返回NID值的Context getter。 – Xdrone

0

我看不到序列器在哪裏構建(也許我錯過了它)。由於它不是一個靜態構造函數,它將不得不被調用至少一次LoadNode才能運行。你是否打算讓這個構造函數也是靜態的?

+0

我已經在構造函數中添加了一條評論。 – Xdrone

+0

添加註釋不會使構造函數爲靜態。看看這個:http://msdn.microsoft.com/en-us/library/k9x6w0hc.aspx – Dweeberly

+0

我回應說:「我看不到序列器正在構建的位置(也許我錯過了它)。」 – Xdrone

7

我在做什麼錯?

您正在等待靜態初始化程序中的另一個線程。 永遠不要這樣做。我無法強調足夠強烈的危險程度。

有關說明爲什麼,看到這個答案:

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

+0

謝謝你的提醒,但現在我的問題是;在這種情況下實施線程並避免鎖定的最佳方法是什麼? – Xdrone

+0

@xdrone:只是不要去那裏。靜態構造函數應該初始化一些靜態字段,這就是它。不要做任何幻想。 –