Announcement

Collapse
No announcement yet.

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 CarlTrading, 03-31-2026, 09:41 PM
        1 response
        42 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by CarlTrading, 04-01-2026, 02:41 AM
        0 responses
        20 views
        0 likes
        Last Post CarlTrading  
        Started by CaptainJack, 03-31-2026, 11:44 PM
        0 responses
        29 views
        1 like
        Last Post CaptainJack  
        Started by CarlTrading, 03-30-2026, 11:51 AM
        0 responses
        46 views
        0 likes
        Last Post CarlTrading  
        Started by CarlTrading, 03-30-2026, 11:48 AM
        0 responses
        38 views
        0 likes
        Last Post CarlTrading  
        Working...
        X