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

MultiThreading Calcs

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

    MultiThreading Calcs

    I've created a class within my NT indicator that processes some data (High, Low, Close, etc...) that's generated from the OnBarUpdate eventHandler and it passes this data to my class to be processed which I've created a managed thread to handle the processing...

    Ninja keeps crashing whenever I attempt to add the indicator to a chart. Is there anything in particular I need to know about as far as using managed threads with NT?


    Example:

    MyCls myCls = new MyCls();

    void OnBarUpdate()
    {
    double high = High[0];
    double low = Low[0];
    double close = Close[0];

    string data = string.Format("{0},{1},{2}",high,low,close);
    myCls.GetData(data);
    }

    // my class
    class MyCls
    {
    EventWaitHandle ewh = new AutoResetEvent(false);
    object sync = new object();
    Queue<string> que = new Queue<string>();
    Thread t = new Thread(worker);

    public MyCls()
    {
    t.IsBackground = true;
    t.Start();
    }

    public void GetData(string data)
    {
    lock(sync)
    {
    que.Enqueue(data);
    }
    ewh.Set();
    }

    private void worker()
    {
    while(true)
    {
    lock(sync)
    {
    // get data from que.Dequeue();
    // blah, blah, blah...
    }

    // process data....blah, blah, blah...
    // after processing data block thread
    ewh.WaitOne();
    }
    }
    Last edited by d.allen101; 12-10-2011, 09:38 AM.

    #2
    d.allen101, I will have someone get back to you on Monday with an answer to your question.
    AustinNinjaTrader Customer Service

    Comment


      #3
      d.allen101, are there any error shown in the log tab when you add your script?
      BertrandNinjaTrader Customer Service

      Comment


        #4
        i know very little about using/debugging NT, someone told to how to get to stack trace and i went to it and found the exception. it was because i was sharing my commandObject and connectionObject across multiple threads. i should have had an exclusive lock on those objects or made them local in scope to each thread. i corrected it so everything in fine now, but thanks for getting back to me.

        Comment


          #5
          Glad to hear you could resolve it!
          BertrandNinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by f.saeidi, Yesterday, 12:14 PM
          9 responses
          23 views
          0 likes
          Last Post f.saeidi  
          Started by Tim-c, Today, 03:54 AM
          0 responses
          3 views
          0 likes
          Last Post Tim-c
          by Tim-c
           
          Started by FrancisMorro, Today, 03:24 AM
          0 responses
          3 views
          0 likes
          Last Post FrancisMorro  
          Started by Segwin, 05-07-2018, 02:15 PM
          10 responses
          1,772 views
          0 likes
          Last Post Leafcutter  
          Started by Rapine Heihei, 04-23-2024, 07:51 PM
          2 responses
          31 views
          0 likes
          Last Post Max238
          by Max238
           
          Working...
          X