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 Mindset, 04-21-2026, 06:46 AM
      0 responses
      90 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by M4ndoo, 04-20-2026, 05:21 PM
      0 responses
      137 views
      0 likes
      Last Post M4ndoo
      by M4ndoo
       
      Started by M4ndoo, 04-19-2026, 05:54 PM
      0 responses
      68 views
      0 likes
      Last Post M4ndoo
      by M4ndoo
       
      Started by cmoran13, 04-16-2026, 01:02 PM
      0 responses
      120 views
      0 likes
      Last Post cmoran13  
      Started by PaulMohn, 04-10-2026, 11:11 AM
      0 responses
      71 views
      0 likes
      Last Post PaulMohn  
      Working...
      X