Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

threadlock - multiframe

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

    threadlock - multiframe

    Dear NT team,

    I am getting the following exception in RC2 when running a strategy:
    Failed to call 'Add' method: System.Threading.LockRecursionException: Write lock may not be acquired with read lock held ...
    Here are the details:
    • multi-instrument strategy;
    • depending on the conditions, on first tick OBU for the primary series uses MessageBox.Show;
    • exception is then thrown and strategy disabled/relevant chart remains frozen;
    • MessageBox.Show is the offending code, if it is removed, no exception is thrown;
    • exception is thrown in real time for a sim account, however, in MarketReplay, no exception is thrown.


    I suspect that the Add method causing the exception is called for BarsArray[>0] trying to add the newly closed bar but the core bars data remains locked as OBU for BarsArray[0] is still waiting for the dialogue to close. With regard to the MarketReplay situation, it is possible that all dataseries are run from the the same thread (I have not checked), so a normal wait for MessageBox.Show to return occurs.

    I do now know if this behaviour is a necessary consequence of NT core implementation or something that can be worked around. I suspect some users might want to have the ability to have modal interaction built in in OBU.

    In any case, please could you let me know if there is a way around the exception (ie, to wait for user input and yet not cause thread lock error for other dataseries).

    Thank you,
    Roman

    #2
    Hello roman_ch,

    Thank you for your post.

    I am not certain you will be able to prevent this by the means you are handling the MessageBox. Why is the MessageBox locking an instrument thread? Is this a dialogue specific to the instrument?

    Can you provide the code or a simplified version that demonstrates how you are locking and releasing the instrument thread?

    Comment


      #3
      Hi Patrick,

      Thanks. Windows.Form Show() is a sync method, so it will block the rest of the code in OBU until return (ie, it waits for user input). My guess is that whilst in OBU for the primary series, NT locks the data that the other instrument thread tries to write to.

      I appreciate I could create an async dialogue and react to its result in later ticks (ie create something of a "handler" in OBU), but it is just a bit more coding than I wanted. Accordingly, I must clarify that I am asking about options other than using an async dialogue.

      And here is the type of code I am using:
      protected override void OnBarUpdate()
      {
      if (State == State.Historical || CurrentBars[0] < ... || BarsInProgress > 0 ... ) return;
      if (IsFirstTickOfBar)
      {
      if (!myTradeTests.RunTests()) return;
      dialogResult = System.Windows.Forms.MessageBox.Show(GetDialogueSt ring(),"Trade?", MessageBoxButtons.YesNo);
      if (dialogResult == DialogResult.Yes) SubmitOrderUnmanaged(iBarsIndex, ...);
      ...
      }
      ...
      }

      Comment


        #4
        Hi roman,

        I use this code to show a NTMessageBox asynchronous. NTMessageBox includes yes/no/cancel options for simple dialogs also.

        Code:
        if (!auth)
                        {
                            var thread = new Thread(new ThreadStart(DisplayMessageBoxThread));
        
                            thread.SetApartmentState(ApartmentState.STA);
                            thread.Start();
                        }
        
        private void DisplayMessageBoxThread()
                {
                    try
                    {
                        NTMessageBox w = new NTMessageBox(@"No está autorizado.", @"Acceso no autorizado", MessageBoxButton.OK, MessageBoxImage.Stop);
                        w.ShowDefault();
                        w.Closed += (s, e) => System.Windows.Threading.Dispatcher.ExitAllFrames();
        
                        System.Windows.Threading.Dispatcher.Run();
                    }
                    catch (Exception ex)
                    {
                        NinjaTrader.Code.Output.Process(ex.Message + Environment.NewLine + ex.StackTrace, PrintTo.OutputTab1);
                    }
                }
        You can substitue the NTMessageBox for a custom dialog window. But you have to build the custom dialog window by xaml and subscribe its events in behind code. Maybe all this could be complex.

        Comment


          #5
          you can collect the return value as follows:

          Code:
          NTMessageBox w = new NTMessageBox(@"Your question ...", @"Tittle", MessageBoxButton.YesNoCancel, MessageBoxImage.Question);
                          MessageBoxResult r = w.ShowDefault();
          
                          switch( r )
                          {
                              case MessageBoxResult.Yes:
                                  break;
                              case MessageBoxResult.No:
                                  break;
                              case MessageBoxResult.Cancel:
                                  break;
                          }

          Comment


            #6
            Thank you, cls71.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by gentlebenthebear, Today, 01:30 AM
            2 responses
            13 views
            0 likes
            Last Post gentlebenthebear  
            Started by Kaledus, Today, 01:29 PM
            2 responses
            7 views
            0 likes
            Last Post Kaledus
            by Kaledus
             
            Started by frankthearm, Yesterday, 09:08 AM
            13 responses
            45 views
            0 likes
            Last Post frankthearm  
            Started by PaulMohn, Today, 12:36 PM
            2 responses
            16 views
            0 likes
            Last Post PaulMohn  
            Started by Conceptzx, 10-11-2022, 06:38 AM
            2 responses
            56 views
            0 likes
            Last Post PhillT
            by PhillT
             
            Working...
            X