Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

OnStateChange Asynchronous Handling

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    OnStateChange Asynchronous Handling

    I've been searching for any guidance on this in the NT8 guide or forum and cannot find anything so far but perhaps I've missed.

    Is OnStateChange part of a proper EventHandler delegate that can be used with the .NET asynchronous await pattern? What is the proper way of performing asynchronous work inside the OnStateChange override? I am currently working inside the AddonBase but I'd like to know how it applies to the Indicator and Strategy use cases also.
    Most normal event handlers in .NET can operate properly when used with the await pattern but I'm not sure if this is appropriate for OnStateChange in this manner.

    Code:
    protected override async void OnStateChange()
    {
        if (State == State.Configure)
        {
            bool result = await SomeAsyncBoolReturn();
        }
    }


    With regard to the AddonBase use case, how does this same question apply to OnWindowCreated / OnWindowDestroyed overrides?

    Code:
    protected override async void OnWindowCreated(Window window)
    {
            object task = await Task.Run(someAsyncFunc);
    }
    ​​

    Thanks in advance.

    #2
    I've been searching for any guidance on this in the NT8 guide or forum and cannot find anything so far but perhaps I've missed.

    Is OnStateChange part of a proper EventHandler delegate that can be used with the .NET asynchronous await pattern? What is the proper way of performing asynchronous work inside the OnStateChange override? I am currently working inside the AddonBase but I'd like to know how it applies to the Indicator and Strategy use cases also.
    Most normal event handlers in .NET can operate properly when used with the await pattern but I'm not sure if this is appropriate for OnStateChange in this manner.

    Code:
    protected override async void OnStateChange()
    {
        if (State == State.Configure)
        {
            bool result = await SomeAsyncBoolReturn();
        }
    }


    With regard to the AddonBase use case, how does this same question apply to OnWindowCreated / OnWindowDestroyed overrides?

    Code:
    protected override async void OnWindowCreated(Window window)
    {
            object task = await Task.Run(someAsyncFunc);
    }
    ​​

    Thanks in advance.

    Comment


      #3
      Hello CDXTrader,

      NinjaScript in general does not support the async/await programming model, everything is executed synchronously however it may be split between multiple threads that call those actions. OnStateChange is called a number of times through the lifecycle of the script but that all happens synchronously. This is the same for every NinjaScript type.

      If you have a piece of C# code or function that is a Task you can choose to run that in two ways. You can call that and then have the result ready at a later time like the code you have shown running the task. The function the task is calling will be run async but the result will not be ready in the method that called that task. That would be helpful for doing long running tasks where you don't need the result right away and will later access the result based on some other event. If you do need the result right away you would need to use the .Result provided by a task that returns a value so it is run synchronously instead of async.

      Code:
      bool result = SomeAsyncBoolReturn().Result;
      If you are making the method yourself like SomeAsyncBoolReturn and it does not use any type of async C# code you should make that a standard method to avoid using tasks at all.
      Code:
      private bool SomeAsyncBoolReturn()  // without async
      {
          return true; 
      }
      JesseNinjaTrader Customer Service

      Comment


        #4
        Hello CDXTrader,

        Thank you for your post.

        The proper way to perform asynchronous work in OnStateChange() would be to use a dispatcher, such as Dispatcher.InvokeAsync(), as mentioned on the page here:


        A dispatcher could be used in an indicator, strategy, or addon script.

        OnStateChanged() is an event-driven method that is called whenever the script enters a new state. The different states are described here, and there is also a list of each NinjaScript type and it's state management system:


        Even more information about the different states may be found on the following page that covers the NinjaScript Lifecycle:


        You should be able to use a dispatcher as needed in OnWindowCreated() and OnWindowDestroyed() as well. Ultimately it depends on the use case and what you are looking to achieve. You may already be familiar with some of the resources I have provided, such as the following page about creating an addon window:


        Please let us know if we may be of further assistance.
        Emily C.NinjaTrader Customer Service

        Comment


          #5
          Copy that, Jesse. Thank you for the quick response.

          I'll plan on using a callback pattern or task queue for long-running functions or stacked processes inside the overrides.​

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by fx.practic, 10-15-2013, 12:53 AM
          5 responses
          5,404 views
          0 likes
          Last Post Bidder
          by Bidder
           
          Started by Shai Samuel, 07-02-2022, 02:46 PM
          4 responses
          95 views
          0 likes
          Last Post Bidder
          by Bidder
           
          Started by DJ888, Yesterday, 10:57 PM
          0 responses
          8 views
          0 likes
          Last Post DJ888
          by DJ888
           
          Started by MacDad, 02-25-2024, 11:48 PM
          7 responses
          159 views
          0 likes
          Last Post loganjarosz123  
          Started by Belfortbucks, Yesterday, 09:29 PM
          0 responses
          8 views
          0 likes
          Last Post Belfortbucks  
          Working...
          X