Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Pass variable from InvokeAsync to outside

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

    Pass variable from InvokeAsync to outside

    How would I retrieve a variable from within Dispatcher.InvokeAsync to use outside of it?

    int GetFixedPosSize()
    {
    int fps = 1;
    Dispatcher.InvokeAsync((() => {
    NinjaTrader.Gui.Tools.QuantityUpDown quantitySelector = (Window.GetWindow(ChartControl.Parent).FindFirst(" ChartTraderControlQuantitySelector") as NinjaTrader.Gui.Tools.QuantityUpDown);

    fps = quantitySelector.Value;
    }));
    return(fps);
    }

    So here, how would I get the changed "fps" value within Dispatcher.InvokeAsync to use outside and return in "return"?

    Thanks.​

    #2
    Hello renner1984,

    Thanks for your post.

    You would need to make "fps" a class-level variable and remove the int fps = 1 variable from the code you shared.

    That method would still be able to be used but you would need to make it a void and not a return method.

    Note that InvokeAsync is not going to be executed immediately. It will execute when the thread allows for it so "fps" could be set later after the logic that calls that method is called. For example, if you called that code in OnBarUpdate, the value might not be available until the next bar or later.

    Further, you should use ChartControl.Dispatcher instead of Dispatcher since that control you are accessing is a control on the ChartControl.

    Multi-Threading Consideration for NinjaScript: https://ninjatrader.com/support/help...-threading.htm

    See this forum thread which shows a private variable and the correct dispatcher: https://forum.ninjatrader.com/forum/...t=2#post787509
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      Like this?

      Anyone knows how to return a value from Dispatcher.Invoke in wpf? I want to return the selected index for a ComboBox. Thanks!
      Bruce DeVault
      QuantKey Trading Vendor Services
      NinjaTrader Ecosystem Vendor - QuantKey

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by NullPointStrategies, Yesterday, 05:17 AM
      0 responses
      54 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      130 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      72 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      44 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      49 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X