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.
private void LockWriteToFile(string _file, string[] _strContents, bool _append) { byte[] data = new byte[0]; foreach (string line in _strContents) {[INDENT]data = data.Concat<byte>(new UTF8Encoding(true).GetBytes(line)).Concat<byte>(ne w UTF8Encoding(true).GetBytes(Environment.NewLine)). ToArray();[/INDENT] } // lock a generic object to ensure only one thread is accessing the following code block at a time lock (lockObj) {[INDENT]using (FileStream fs = new FileStream (_file, _append? FileMode.Append:FileMode.Create, FileAccess.Write, FileShare.None) ) {[/INDENT][INDENT=2]fs.Write(data, 0, data.Length); fs.Flush();[/INDENT][INDENT]}[/INDENT] } }
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.
Comment