Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Read file asynch

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

    Read file asynch

    Hello,
    I would like read file asynchronously or at least, close the file when I will end to read. I'am trying like here:
    - http://msdn.microsoft.com/en-us/library/jj155757.aspx
    - http://msdn.microsoft.com/en-us/libr...code-snippet-2
    - http://msdn.microsoft.com/en-us/libr...v=vs.110).aspx

    But I can not find the way to do it. Can someone help me?

    Thank you in advance!

    #2
    ferran, thanks for the post - do you mean you want to have concurrent access to one file from multiple scripts?

    There's a discussion on it already here on our forums - http://www.ninjatrader.com/support/f...ad.php?t=66007

    The async / await patterns will need a higher .NET framework version that would currently would be supported with NT7 - our next major platform update will extend that to 4.5

    Comment


      #3
      Hello,
      I will have a file that will be written by other application and I need that NT7 can read it without blocking it. How can I do it?

      Thank you so much in advance,
      Regards

      Comment


        #4
        Please check into the other thread I pointed to, in short you want to ensure the file has be opened correctly to allow concurrent access.

        Comment


          #5
          Hello again!
          Yes, thank you. I have solved.

          The code that works:

          <#region Using declarations
          using System;
          using System.ComponentModel;
          using System.Diagnostics;
          using System.Drawing;
          using System.Drawing.Drawing2D;
          using System.Xml.Serialization;
          using NinjaTrader.Cbi;
          using NinjaTrader.Data;
          using NinjaTrader.Indicator;
          using NinjaTrader.Gui.Chart;
          using NinjaTrader.Strategy;
          #endregion

          // Add this to your declarations to use StreamWriter and StreamReader
          using System.IO;
          using System.Windows;
          using System.Text;

          // This namespace holds all strategies and is required. Do not change it.
          namespace NinjaTrader.Strategy
          {
          /// <summary>
          ///
          /// </summary>
          [Description("")]
          public class MCOrderclientNT7 : Strategy
          {
          #region Variables
          // This sets the path in which the text file will be created.
          private string path = "T:\\order.txt";
          private FileStream fs;
          // Order
          private double order = 0;
          #endregion

          /// <summary>
          /// This method is used to configure the indicator and is called once before any bar data is loaded.
          /// </summary>
          protected override void Initialize()
          {
          CalculateOnBarClose = false;
          }

          /// <summary>
          /// Called on each bar update event (incoming tick)
          /// </summary>
          protected override void OnBarUpdate()
          {
          if (Historical)
          return;
          // Checks to see if the file exists
          else if (File.Exists(path))
          {

          byte[] buffer;
          FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.ReadWrite);
          try
          {
          string fileContents;
          using (StreamReader reader = new StreamReader(fileStream))
          {
          fileContents = reader.ReadLine();
          Print("===>" + fileContents);
          }
          }
          finally
          {
          fileStream.Close();
          }
          }
          // If file does not exist, let the user know
          else
          Print("File does not exist.");
          } //end OnBarUpdate

          #region Properties

          #endregion
          }
          }

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by NullPointStrategies, Today, 05:17 AM
          0 responses
          53 views
          0 likes
          Last Post NullPointStrategies  
          Started by argusthome, 03-08-2026, 10:06 AM
          0 responses
          130 views
          0 likes
          Last Post argusthome  
          Started by NabilKhattabi, 03-06-2026, 11:18 AM
          0 responses
          70 views
          0 likes
          Last Post NabilKhattabi  
          Started by Deep42, 03-06-2026, 12:28 AM
          0 responses
          44 views
          0 likes
          Last Post Deep42
          by Deep42
           
          Started by TheRealMorford, 03-05-2026, 06:15 PM
          0 responses
          49 views
          0 likes
          Last Post TheRealMorford  
          Working...
          X