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 wzgy0920, 04-20-2024, 06:09 PM
        2 responses
        26 views
        0 likes
        Last Post wzgy0920  
        Started by wzgy0920, 02-22-2024, 01:11 AM
        5 responses
        32 views
        0 likes
        Last Post wzgy0920  
        Started by wzgy0920, Yesterday, 09:53 PM
        2 responses
        49 views
        0 likes
        Last Post wzgy0920  
        Started by Kensonprib, 04-28-2021, 10:11 AM
        5 responses
        192 views
        0 likes
        Last Post Hasadafa  
        Started by GussJ, 03-04-2020, 03:11 PM
        11 responses
        3,235 views
        0 likes
        Last Post xiinteractive  
        Working...
        X