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

Is it safe at all time to update the GUI in NinjaScript without using a dispatcher?

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

    Is it safe at all time to update the GUI in NinjaScript without using a dispatcher?

    A few times Iīve hade the error:


    The calling thread cannot access this object because a different thread owns it.



    I assume itīs safe at all time to update the GUI in NinjaScript without using a dispatcher?



    Code:
    
    [LEFT][COLOR=#4D4D4D][FONT=Helvetica][SIZE=13px]  protected override void OnBarUpdate()[/SIZE][/FONT][/COLOR][/LEFT]
    [LEFT][COLOR=#4D4D4D][FONT=Helvetica][SIZE=13px]  {[/SIZE][/FONT][/COLOR][/LEFT]
    [LEFT][COLOR=#4D4D4D][FONT=Helvetica][SIZE=13px]   [/SIZE][/FONT][/COLOR][/LEFT]
    [LEFT][COLOR=#4D4D4D][FONT=Helvetica][SIZE=13px]   [/SIZE][/FONT][/COLOR][/LEFT][LEFT][COLOR=#4D4D4D][FONT=Helvetica][SIZE=13px]Draw.Text(//anything);[/SIZE][/FONT][/COLOR][/LEFT]
    [LEFT][COLOR=#4D4D4D][FONT=Helvetica][SIZE=13px]   ForceRefresh();[/SIZE][/FONT][/COLOR][/LEFT]
    [LEFT][COLOR=#4D4D4D][FONT=Helvetica][SIZE=13px]   [/SIZE][/FONT][/COLOR][/LEFT]
    
    [LEFT][COLOR=#4D4D4D][FONT=Helvetica][SIZE=13px]  }
    
    [/SIZE][/FONT][/COLOR][/LEFT]


    Kindly, Fredrik

    #2
    Hello Fredrik,

    Thanks for your post.

    Calling Draw methods is perfectly appropriate to be called in OnBarUpdate. Calling ForceRefesh does not have to be called from a UI thread and can be called anywhere.

    Modifications to the GUI should be done through a dispatcher. Some examples for appropriate GUI modification are linked below.

    WPF Modification examples - https://ninjatrader.com/support/foru...considerations

    Multi Threading Considerations - https://ninjatrader.com/support/help...-threading.htm

    To troubleshoot the behavior you have further, I suggest finding a reproducible context to hit the error, and then to comment sections of your code to determine which part of your code is causing this issue. If you have any specific questions on that set of code, I will be happy to share my thoughts.

    I look forward to assisting.
    Last edited by NinjaTrader_Jim; 10-23-2019, 10:51 AM.
    JimNinjaTrader Customer Service

    Comment


      #3
      Thanks for pointing me to this information.

      I have some non NinjaScript code in OnBarUpdate() that is triggered at certain conditions. Itīs non GUI related, has no returning value and is started in another tread with Task.Run()… which I supposed would run without interfering with NT threads.

      Stil I get this error:

      Error on calling 'OnBarUpdate' method on bar 23993: The calling thread cannot access this object because a different thread owns it.

      Whatīs going on here? What would be the way to go?

      Kindly, Fredrik
      Last edited by FREEN; 10-24-2019, 10:55 AM.

      Comment


        #4
        Hello Fredrik,

        Thanks for your reply.

        I would need to get a better idea for what you are doing with Task.Run to hit this error to give any further insight. Custom multi threaded routines begin to escape the scope of support that we can provide, but I believe we can get you going in the right direction if we saw a small example of what you are trying to accomplish.

        Could you share a small testable example that shows what you are trying to accomplish and can hit the error?

        I look forward to assisting.
        JimNinjaTrader Customer Service

        Comment


          #5
          Iīll do some more debugging before I turn to your generous help.

          Iīm I correct that this error only would occour if I interfer with the GUI thread?

          Comment


            #6
            Hello Fredrik,

            This error would be related to accessing anything from an improper thread. This could be accessing GUI items outside of a UI thread, or accessing other NinjaScript properties which reside on the Instrument thread outside of that thread.

            OnRender and OnRenderTargetChanged reside on the UI thread and realtime data operations reside on an Instrument thread. Historical operations in OnBarUpdate will be performed through a thread pool.

            I will be happy to look at a small example to provide further direction if it is needed.
            JimNinjaTrader Customer Service

            Comment


              #7
              I had a Label method (GUI component) beeing updated from OnBarUpdate() and a timer eventhandler, OnSecondEvent(). It seems my threading issue disapear (at least no errors yet) after deleting the updating of the Label method from the OnBarUpdate() letting it beeing updated from the timer evenhander only.

              So, I guess OnBarUpdate() runs on the GUI thread and OnSecondEvent() on a threadpool? If so it wouldnīt be safe to run the DrawLabels() from the OnSecondEvent() either?

              So, am I asking for threading trouble with this code (that seem to run without issues)?

              Code:
              
                // A local System.Timers.Timer
                protected void [B]OnSecondEvent[/B](object sender, ElapsedEventArgs e)
                {
              
                 DrawLabels();
                }
              
                protected override void [B]OnBarUpdate()[/B]
                {
              
                 //DrawLabels(); // Conflict between OnBarUpdate() and OnSecondEvent() when updating this method/GUI component???
              
                }
              
              
                protected void [B]DrawLabels()[/B]
                {
                    if (CurrentBar != Bars.Count-2) return; // Draw on last bar only
                    string labelText = string.Empty;
              
                 // is DrawLabels() causing theading issue?
                 try
                 {
                     labelText =
                     "\t\t\t\t\t"
                     + NinjaTraderLabel()
              [COLOR=#0000CD]       + priceBars.Label[/COLOR]
                     + SessionLabel()
              [COLOR=#0000CD]       + wac.AutomationLabel // proprietary assemblies code
                     + wac.AccountLabel
                     + tlc.Label;[/COLOR]
                 }
                 catch (Exception e)
                 {
                     labelText = e.ToString();
                 }
              
              
              [COLOR=#008000]      Draw.TextFixed(this, "label", labelText… // NinjaScript code
                    ForceRefresh();[/COLOR]
                }
              Last edited by FREEN; 10-26-2019, 09:15 AM.

              Comment


                #8
                Hello Fredrik,

                The script cannot be compiled and tested in the current state. I have included an example that calls drawing objects from a timer and from OnBarUpdate. Please test this example to see if it can move you forward and if you are still stuck, please attach an export of a reduced example that I can test on my end.

                Export - https://ninjatrader.com/support/help...tAsSourceFiles

                I look forward to assisting.
                Attached Files
                JimNinjaTrader Customer Service

                Comment


                  #9
                  Jim, thatīs more help than I expected and asked for... thank you very much. My code was only intended as pseudocode and Iīm still very confused with NT threading (and threading in general). I have some food for thought and startingpoints for further readings to wrap my head around the issue now.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by stafe, 04-15-2024, 08:34 PM
                  10 responses
                  42 views
                  0 likes
                  Last Post stafe
                  by stafe
                   
                  Started by frslvr, 04-11-2024, 07:26 AM
                  7 responses
                  111 views
                  1 like
                  Last Post caryc123  
                  Started by rocketman7, Today, 09:41 AM
                  3 responses
                  8 views
                  0 likes
                  Last Post NinjaTrader_Jesse  
                  Started by traderqz, Today, 09:44 AM
                  2 responses
                  5 views
                  0 likes
                  Last Post NinjaTrader_Gaby  
                  Started by rocketman7, Today, 02:12 AM
                  7 responses
                  31 views
                  0 likes
                  Last Post NinjaTrader_ChelseaB  
                  Working...
                  X