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

Problem with Dispatcher.InvokeAsync

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

    Problem with Dispatcher.InvokeAsync

    Hi

    I'm finding and getting a window state with:

    Code:
    // Loop through all NT windows to find the SuperDOM window
    foreach (var window in NinjaTrader.Core.Globals.AllWindows)
    {[INDENT]// Check if the found window is the one required
    if (!(window is NinjaTrader.Gui.SuperDom.SuperDom)) continue;
    
    window.Dispatcher.InvokeAsync(new Action(() =>
    {[/INDENT][INDENT=2]// Try to cast as the required type of window, if it fails it will be null
    foundSuperDom = window as NinjaTrader.Gui.SuperDom.SuperDom;
    
    // Check we found a SuperDOM
    if (foundSuperDom == null) return;
    
    // Restore/minimize SuperDOM window
    if (foundSuperDom.WindowState == System.Windows.WindowState.Minimized)
    {[/INDENT][INDENT=3]foundSuperDom.WindowState = System.Windows.WindowState.Normal;
    isSuperDomMinimized = false;[/INDENT][INDENT=2]}
    else
    {[/INDENT][INDENT=3]foundSuperDom.WindowState = System.Windows.WindowState.Minimized;
    isSuperDomMinimized = true;[/INDENT][INDENT=2]}[/INDENT][INDENT]}));[/INDENT]
     
     }
    
    Print(isSuperDomMinimized);
    without any issue.

    The problem I have is
    Code:
    Print(isSuperDomMinimized);
    runs before the looping through the windows. How I do I get the Print statement to run after the looping through the windows?

    #2
    Not to worry, I've found the solution.
    It's all down to accessing the correct thread using the correct dispatcher.

    Comment


      #3
      Hello FatCanary,

      Dispatcher.InvokeAsync is asynchronous. This means the code is running on another thread. If you want a thread blocking invoke you can use Dispatcher.Invoke(), however keep in mind this can cause deadlocks.
      Chelsea B.NinjaTrader Customer Service

      Comment


        #4
        Yep, thanks Chelsea.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by thanajo, 05-04-2021, 02:11 AM
        3 responses
        469 views
        0 likes
        Last Post tradingnasdaqprueba  
        Started by Christopher_R, Today, 12:29 AM
        0 responses
        10 views
        0 likes
        Last Post Christopher_R  
        Started by sidlercom80, 10-28-2023, 08:49 AM
        166 responses
        2,237 views
        0 likes
        Last Post sidlercom80  
        Started by thread, Yesterday, 11:58 PM
        0 responses
        4 views
        0 likes
        Last Post thread
        by thread
         
        Started by jclose, Yesterday, 09:37 PM
        0 responses
        9 views
        0 likes
        Last Post jclose
        by jclose
         
        Working...
        X