Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Thread problem and compile issue

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

    Thread problem and compile issue

    Hi ,
    I need some help with two issues.
    1) I currently have version 8.1.2.1 installed and up till 10 minutes ago it was fine. I commented out a bunch of print statements trying to solve my other issue and I hit compile and it is still compiling. I don't want to crash it. How do I recover from this? I don't want the newest version. Can I go back to one previous to 8.1.2.1 ?
    2) I seem to have a thread issue. This seems to have just shown up but I discovered it quite by accident.
    My code is a strategy with buttons on chart trader.
    The button event handlers only set flags.
    In Onbarupdate() the flags are read. It then generates an atmStrategy based on the buttonClick event flag.
    Calculate is set to OnBarclose
    if I repeatedly button click thru the close of a bar it will generate an error.
    "Strategy 'GuiTrader': Error on calling 'OnBarUpdate' method on bar 479: The calling thread cannot access this object because a different thread owns it."
    I commented out all the code in the buttonclick event handler but it still generates the error if I repeatedly click while the close of the bar occurs. Any other time during the bar it
    does not generate an error if I repeatedly click. that tells me the its happening in OnbarUpdate but the association between OnbarUpdate and the Button has been commented out.
    I am befuddled as to how to debug this since I do not have any code in the button yet it only happens while clicking the button.

    My editor is still compiling. It must be 30 minutes now. Do I kill it ?

    Jerry

    #2
    Hello JerryWar,

    Thank you for your post.

    We recommend updating to 8.1.3.0, as 8.1.2 had an issue where the compilation would take a very long time to complete which was resolved in the latest version.

    Are you calling ChartControl.Dispatcher.InvokeAsync when accessing the button? This is necessary because the NinjaScript object runs on a non-UI thread:



    Please let us know if we can assist further.

    Comment


      #3
      Thanks Gaby. I killed the process after 1.5 hours and installed the newest version.

      Comment


        #4
        I used Dispatcher.InvokeAsync in the following manner.

        ChartControl.Dispatcher.InvokeAsync(() =>
        {
        BuyButtonPress=BuyButtonClick;
        });​

        BuyButtonClick is a boolean set or reset in the BuyButton event handler. I am trying to access it in OnBarUpdate.
        the problem is that the assignment to BuyButtoPress lags by 1 bar from the bar in which the asynchronous buttonclick event occurs.
        Printing the time that BuyButtonPress gets set and BuyButtonClick gets set in Onbarupdate confirms this behaviour.
        That doesn't work for me. The only way I see around it is to place the whole section of code that needs BuyButtonClick within the dispatcher.InvokeAsync
        and that didn't work. I am missing something here.
        Can anyone suggest a way to get this event into Onbarupdate in a timely manner.
        Thanks

        Comment


          #5
          Hello JerryWar,

          Toggling a bool inside of a buttons event handler would not require using a dispatcher, the button event is on the UI thread with the button and no UI control is being accessed by your code. You can see an example of how a bool is used in a button event without dispatching here:



          ​If your code is similar to that script the reason it is waiting for the next bar is that the bool is used in OnBarUpdate, the bool is toggled immediately when the button is pressed but if the script is set to Calculate OnBarClose that means the bool won't be used until the next bar close. To have it react immediately you would need to use Calculate OnEachTick so the logic in OnBarUpdate is checking the bool value on each tick.

          Comment


            #6
            Jesse Thanks, Turns out my code is exactly the same format using the bool the same way. I don't understand the issue I am having with exceptions occurring when I don't have the dispatcher. I will remove it and retest. I need to reread the doc and understand this.

            I understand about Calculate OnEachTick. What I don't understand is why, when the button is clicked in the middle of the bar, before bar close, why the information doesn't transfer on That bar. close i.e. it has to wait another bar.

            Jerry

            Comment


              #7
              Hello JerryWar,

              Without seeing a sample of what you made it would just be a guess on what's happening. The best way forward would be to compare what you have to the script I linked, that uses OnEachTick so it can react in the middle of a bar when clicked however it does have other entry conditions so it may wait some time before entering. You can remove those conditions so its just the bool being checked in OnBarUpdate to see it react instantly. If you still have problems attach a small sample that shows what code you used.

              If you are getting threading exceptions that means something on the UI is being manipulated in addition to toggling the bool, if you have other logic in your button event besides the bool it might be due to that logic.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by NullPointStrategies, Today, 05:17 AM
              0 responses
              43 views
              0 likes
              Last Post NullPointStrategies  
              Started by argusthome, 03-08-2026, 10:06 AM
              0 responses
              124 views
              0 likes
              Last Post argusthome  
              Started by NabilKhattabi, 03-06-2026, 11:18 AM
              0 responses
              65 views
              0 likes
              Last Post NabilKhattabi  
              Started by Deep42, 03-06-2026, 12:28 AM
              0 responses
              42 views
              0 likes
              Last Post Deep42
              by Deep42
               
              Started by TheRealMorford, 03-05-2026, 06:15 PM
              0 responses
              46 views
              0 likes
              Last Post TheRealMorford  
              Working...
              X