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 Stanfillirenfro, Today, 07:23 AM
        9 responses
        23 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by George21, Today, 10:07 AM
        0 responses
        6 views
        0 likes
        Last Post George21  
        Started by DayTradingDEMON, Today, 09:28 AM
        2 responses
        18 views
        0 likes
        Last Post DayTradingDEMON  
        Started by navyguy06, Today, 09:28 AM
        1 response
        6 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Started by cmtjoancolmenero, Yesterday, 03:58 PM
        8 responses
        32 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Working...
        X