Access persistent data

@DavidRutten and @TomTom thanks for your replies.

I tried modifying my code to listen listen to when the input parameter has expired but, the event does not seem to be triggering, and I guess that is do to the fact of the order of computation both of you have mentioned. So does this check have to be async?

Here is the new code


 private void RunScript()
  {


    GH_Component comp = null;
    for (int i = 0; i < GrasshopperDocument.Objects.Count; i++)
    {
      if(GrasshopperDocument.Objects[i].Name == "Environment Dimension ")
      {

        comp = GrasshopperDocument.Objects[i] as GH_Component;
        Print("Component name:" + " " + comp.Name);
        param = comp.Params.Input[0].Sources[0] as GH_PersistentParam<GH_Integer>;
        Print(param.Name);

        // subscribe event
        param.SolutionExpired += Param_SolutionExpired;
        

        break;

      }
    }



  }

  // <Custom additional code> 
  GH_PersistentParam<GH_Integer> param = null;

  GH_Structure<GH_Integer> persistentData = null;



  private void Param_SolutionExpired(IGH_DocumentObject sender, GH_SolutionExpiredEventArgs e)
  {
    // Assign persistent data value
    persistentData = param.PersistentData;
    Print(persistentData.ToString());
    Component.ExpireSolution(true);
  }

Also just as a prove of concept I generated a small event handling code to just make sure I was not missing something obvious. As soon as it recognizes an input value of 2 it will fire the event.

 private void RunScript(int x, ref object A)
  {
    RecogniseNum2 rec = new RecogniseNum2();

    rec.foundNumber2 += rec_checking;
    rec.Check(x);

  }

  // <Custom additional code> 

  private void rec_checking(object sender, EventArgs e)
  {
    Print("Found number 2");
  }

  public class RecogniseNum2
  {
    public event EventHandler foundNumber2;

    public void Check(int x)
    {

      if ((x == 2) && (foundNumber2 != null))
      {
        foundNumber2(this, EventArgs.Empty);
      }

    }
  }

So what I am suspecting is that maybe its something along the lines of async event patterns?
if it is I have not yet dealt with async programming, just read some stuff about it.