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 CaptainJack, 05-29-2026, 05:09 AM
      0 responses
      173 views
      0 likes
      Last Post CaptainJack  
      Started by CaptainJack, 05-29-2026, 12:02 AM
      0 responses
      90 views
      0 likes
      Last Post CaptainJack  
      Started by charlesugo_1, 05-26-2026, 05:03 PM
      0 responses
      129 views
      0 likes
      Last Post charlesugo_1  
      Started by DannyP96, 05-18-2026, 02:38 PM
      1 response
      208 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by CarlTrading, 05-11-2026, 05:56 AM
      0 responses
      186 views
      0 likes
      Last Post CarlTrading  
      Working...
      X