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

Access Violation Exception example not working

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

    Access Violation Exception example not working

    I am using the lock() and FileStream example from section Access Violation Exception on page... https://ninjatrader.com/support/help...-threading.htm

    The example hasn't solved the issue. Errors are still happening.

    In my testing, I have 4 instances of the same indicator all needing to write to the same file during State.Historical and State.Realtime. During State.Historical errors are thrown all the time. Shown below. State.Realtime has not thrown any error yet.


    30|***** Wrote to ex. file: AutoSizeRenko setting NQ 20210615.csv Replaced line 2 with text: 30, 07:30:00, 162, PTO-3 time = Tuesday 6/15/2021 7:30:00 AM
    60|***** Wrote to ex. file: AutoSizeRenko setting NQ 20210615.csv Replaced line 1 with text: 60, 07:30:00, 179, PTO-3 time = Tuesday 6/15/2021 7:30:00 AM
    30|***** Wrote to ex. file: AutoSizeRenko setting NQ 20210616.csv Replaced line 2 with text: 30, 07:30:00, 146, PTO-3 time = Wednesday 6/16/2021 7:30:00 AM
    VOLATILITY SIZER V4 FAILED to write 'NQ 20210615.csv, 15, 07:30:00' to file! >>> System.IO.IOException: The process cannot access the file 'C:\Users\End User\Documents\NinjaTrader 8\db\tick\AutoSizeRenko setting NQ 20210615.csv' because it is being used by another process.
    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
    at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
    at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
    at NinjaTrader.NinjaScript.Indicators.JATSIndicators. VolatilitySizer.LockWriteToFile(String _file, String[] _strContence, Boolean _append)
    at NinjaTrader.NinjaScript.Indicators.JATSIndicators. VolatilitySizer.WriteData(String _paf, Int32 _RevSize, Int32 _timeRange, DateTime _time, String _pto)
    VOLATILITY SIZER V4 FAILED to write 'NQ 20210615.csv, 10, 07:30:00' to file! >>> System.IO.IOException: The process cannot access the file 'C:\Users\End User\Documents\NinjaTrader 8\db\tick\AutoSizeRenko setting NQ 20210615.csv' because it is being used by another process.
    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
    at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
    at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
    at NinjaTrader.NinjaScript.Indicators.JATSIndicators. VolatilitySizer.LockWriteToFile(String _file, String[] _strContence, Boolean _append)
    at NinjaTrader.NinjaScript.Indicators.JATSIndicators. VolatilitySizer.WriteData(String _paf, Int32 _RevSize, Int32 _timeRange, DateTime _time, String _pto)
    30|***** Wrote to ex. file: AutoSizeRenko setting NQ 20210617.csv Replaced line 3 with text: 30, 07:30:00, 151, PTO-3 time = Thursday 6/17/2021 7:30:00 AM
    60|***** Wrote to ex. file: AutoSizeRenko setting NQ 20210616.csv Replaced line 1 with text: 60, 07:30:00, 168, PTO-3 time = Wednesday 6/16/2021 7:30:00 AM
    VOLATILITY SIZER V4 FAILED to write 'NQ 20210616.csv, 15, 07:30:00' to file! >>> System.IO.IOException: The process cannot access the file 'C:\Users\End User\Documents\NinjaTrader 8\db\tick\AutoSizeRenko setting NQ 20210616.csv' because it is being used by another process.
    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
    at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
    at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
    at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize, Boolean checkHost)
    at System.IO.StreamReader..ctor(String path, Encoding encoding)
    at System.IO.File.InternalReadAllLines(String path, Encoding encoding)
    at NinjaTrader.NinjaScript.Indicators.JATSIndicators. VolatilitySizer.WriteData(String _paf, Int32 _RevSize, Int32 _timeRange, DateTime _time, String _pto)
    10|***** Wrote to ex. file: AutoSizeRenko setting NQ 20210616.csv Replaced line 3 with text: 10, 07:30:00, 41, PTO-2 time = Wednesday 6/16/2021 7:30:00 AM
    60|***** Wrote to ex. file: AutoSizeRenko setting NQ 20210617.csv Replaced line 4 with text: 60, 07:30:00, 173, PTO-3 time = Thursday 6/17/2021 7:30:00 AM

    This is the method that writes to the file.
    Code:
    private void LockWriteToFile(string _file, string[] _strContents, bool _append)
    {
    byte[] data = new byte[0];
    foreach (string line in _strContents) {
    data = data.Concat<byte>(new UTF8Encoding(true).GetBytes(line)).Concat<byte>(ne w UTF8Encoding(true).GetBytes(Environment.NewLine)). ToArray();
    } // lock a generic object to ensure only one thread is accessing the following code block at a time lock (lockObj) {
    using (FileStream fs = new FileStream (_file, _append? FileMode.Append:FileMode.Create, FileAccess.Write, FileShare.None) ) {
    fs.Write(data, 0, data.Length); fs.Flush();
    }
    } }
    LockWriteToFile() is called within another "pre-check" method that checks if the file exists to determine if the data needs to be appended or not. Also, that pre-check method processes the text data prior to passing it into LockWriteToFile().

    Is the example code from Multi-Threading Consideration for NinjaScript missing something? Does the lock() need to be moved into the "pre-check" method?
    Thanks.

    #2
    Hi Zachary, thanks for posting.

    The lock object would prevent any other thread from accessing the file, and if an other thread does try to access it while locked, that thread will wait until the object is unlocked to continue, so I do not think the exception is coming from within the lock (lockObj) block. You can use Visual Studio to attach to the NinjaTrader process and see the exact line that is causing the exception, see here for instructions:
    https://ninjatrader.com/support/help..._debugging.htm

    Best regards,
    -ChrisL
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      I moved the Try-Catch into the LockWriteToFile() method to narrow where the error is coming from. The error is coming from the .Write command inside of LockWriteToFile().

      1) Is there a threading difference during State.Historical vs. Realtime?
      2) During State.Realtime are all instances of the indicator running on the same thread, but during State.Historical they are running on different threads?
      Can you help me understand why the issue is only occurring during State.Historical.


      Code:
      rivate void LockWriteToFile(string _file, string[] _strContents, bool _append)
      {
      byte[] data = new byte[0];
      foreach (string line in _strContents) {
      data = data.Concat<byte>(new UTF8Encoding(true).GetBytes(line)).Concat<byte>(ne w UTF8Encoding(true).GetBytes(Environment.NewLine)). ToArray();
      }
      
      try {
      // lock a generic object to ensure only one thread is accessing the following code block at a time
      lock (lockObj) {
      using (FileStream fs = new FileStream (_file, _append? FileMode.Append:FileMode.Create, FileAccess.Write, FileShare.None) ) {
      fs.Write(data, 0, data.Length); fs.Flush();
      } } } catch(Exception ex) { string[] fileNameParts = _file.Split('\\').Last().Split(' '); string instDate = fileNameParts[2]+" "+fileNameParts[3].Split('.').First(); Print(thisName.ToUpper()+" ERROR writing data '"+instDate+", "+timeRange+"' to file! >>> "+ex.ToString()); } }

      Errors during State.Historical
      30|***** Wrote to ex. file: AutoSizeRenko setting NQ 20210615.csv Replaced line 2 with text: 30, 07:30:00, 162, PTO-3 time = Tuesday 6/15/2021 7:30:00 AM
      15|***** Wrote to ex. file: AutoSizeRenko setting NQ 20210615.csv Added to end of file Text: 15, 07:30:00, 55, PTO-2 time = Tuesday 6/15/2021 7:30:00 AM
      30|***** Wrote to ex. file: AutoSizeRenko setting NQ 20210616.csv Replaced line 2 with text: 30, 07:30:00, 146, PTO-3 time = Wednesday 6/16/2021 7:30:00 AM
      VOLATILITY SIZER V4 ERROR writing data 'NQ 20210615, 60' to file! >>> System.IO.IOException: The process cannot access the file 'C:\Users\End User\Documents\NinjaTrader 8\db\tick\AutoSizeRenko setting NQ 20210615.csv' because it is being used by another process.
      at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
      at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
      at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
      at NinjaTrader.NinjaScript.Indicators.JATSIndicators. VolatilitySizer.LockWriteToFile(String _file, String[] _strContents, Boolean _append)
      60|***** Wrote to ex. file: AutoSizeRenko setting NQ 20210615.csv Replaced line 1 with text: 60, 07:30:00, 179, PTO-3 time = Tuesday 6/15/2021 7:30:00 AM
      60|***** Wrote to ex. file: AutoSizeRenko setting NQ 20210616.csv Replaced line 1 with text: 60, 07:30:00, 168, PTO-3 time = Wednesday 6/16/2021 7:30:00 AM
      30|***** Wrote to ex. file: AutoSizeRenko setting NQ 20210617.csv Replaced line 3 with text: 30, 07:30:00, 151, PTO-3 time = Thursday 6/17/2021 7:30:00 AM
      VOLATILITY SIZER V4 ERROR writing data 'NQ 20210616, 15' to file! >>> System.IO.IOException: The process cannot access the file 'C:\Users\End User\Documents\NinjaTrader 8\db\tick\AutoSizeRenko setting NQ 20210616.csv' because it is being used by another process.
      at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
      at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
      at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
      at NinjaTrader.NinjaScript.Indicators.JATSIndicators. VolatilitySizer.LockWriteToFile(String _file, String[] _strContents, Boolean _append)


      No errors during State.Realtime
      15|***** Wrote a new file: AutoSizeRenko setting NQ 20210618.csv Text: 15, 07:30:00, 59, PTO-2 time = Friday 6/18/2021 7:30:00 AM
      30|***** Wrote to ex. file: AutoSizeRenko setting NQ 20210618.csv Added to end of file Text: 30, 07:30:00, 160, PTO-3 time = Friday 6/18/2021 7:30:00 AM
      60|***** Wrote to ex. file: AutoSizeRenko setting NQ 20210618.csv Added to end of file Text: 60, 07:30:00, 197, PTO-3 time = Friday 6/18/2021 7:30:00 AM
      30|***** Wrote to ex. file: AutoSizeRenko setting NQ 20210618.csv Replaced line 2 with text: 30, 07:30:00, 160, PTO-3 time = Friday 6/18/2021 7:30:00 AM
      60|***** Wrote to ex. file: AutoSizeRenko setting NQ 20210618.csv Replaced line 3 with text: 60, 07:30:00, 197, PTO-3 time = Friday 6/18/2021 7:30:00 AM
      10|***** Wrote to ex. file: AutoSizeRenko setting NQ 20210618.csv Added to end of file Text: 10, 07:30:00, 44, PTO-2 time = Friday 6/18/2021 7:30:00 AM
      10|***** Wrote to ex. file: AutoSizeRenko setting NQ 20210618.csv Replaced line 4 with text: 10, 07:30:00, 44, PTO-2 time = Friday 6/18/2021 7:30:00 AM
      15|***** Wrote to ex. file: AutoSizeRenko setting NQ 20210618.csv Replaced line 1 with text: 15, 07:30:00, 59, PTO-2 time = Friday 6/18/2021 7:30:00 AM

      Comment


        #4
        Hi Zachary,

        I wanted to suggest a new way of implementing the lock object because I'm thinking our documentation is not mentioning the lock object needs to be a shared object amongst the threads. The sample is creating a new lock object but does not mention other instances needing to share this object. I added a .cs file in my Addon that is a global static class holding the generic lock object. I've shared the code for you to try. Add the attached file into Documents\NinjaTrader 8\bin\Custom\AddOns and in the script that uses the lock object add this:

        using NinjaTrader.NinjaScript.AddOns;
        and
        lock (Global.lockObj)

        If this does not resolve, I will need you to make a version of the script that is reduced down to only the code causing the error so I can reproduce on my machine.

        Best regards.

        Attached Files
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          Good catch. I meant to ask if the lockObj should be static, but slipped my mind.
          It is working now during State.Historical. I'll have to wait until Monday's market open to see if there is any issue during State.Realtime.
          thanks

          Comment


            #6
            Hi Chris,
            I spoke to soon. An error is still happening.
            Indicator 'Volatility Sizer v4': Error on calling 'OnBarUpdate' method on bar 4234: The process cannot access the file 'C:\Users\End User\Documents\NinjaTrader 8\db\tick\AutoSizeRenko setting NQ 20210616.csv' because it is being used by another process.

            Where should I email the indicator to?

            Comment


              #7
              Hi Zachary,

              Please reduce the script down to only demonstrate the error at hand. You may send it to scriptingsupport at ninjatrader.com and reference "Attn ChrisL 3147498". In the body of the email please also list out how to reproduce the error.

              Best regards.
              Chris L.NinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by WHICKED, Today, 04:10 PM
              0 responses
              8 views
              0 likes
              Last Post WHICKED
              by WHICKED
               
              Started by Trasss_Kenneth, Today, 01:49 PM
              2 responses
              38 views
              0 likes
              Last Post bltdavid  
              Started by Newtrader101, Today, 03:22 PM
              2 responses
              15 views
              0 likes
              Last Post Newtrader101  
              Started by johnMoss, Today, 01:43 PM
              5 responses
              39 views
              0 likes
              Last Post NinjaTrader_ChrisL  
              Started by qs121, Today, 12:24 PM
              2 responses
              46 views
              0 likes
              Last Post qs121
              by qs121
               
              Working...
              X