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

Write to same text file from two different sources

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

    Write to same text file from two different sources

    Hi,

    I am using the following method to write to a text file and would like to know what I need to change to be able to write to the same text file without seeing 'the process cannot access the file because it is being used by another process' error message.

    Code:
    using System.IO;
    
    File.AppendAllText()
    Regards,
    suprsnipes
    Last edited by suprsnipes; 11-25-2012, 11:07 PM.

    #2
    Do you mean writing to the same txt file from two or more different script / strategies? With no cross communication this would not be easily possible, since you would need to implement a mutex / lock concept to be able to ensure no concurrent access would be attempted.
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Hi Bertrand,

      Thanks for your suggestions I'm thinking that I may need to try and achieve something less difficult at first. What about allowing the text file that I am writing to access for another process to read?

      Regards,
      suprsnipes

      Comment


        #4
        Originally posted by suprsnipes View Post
        Hi Bertrand,

        Thanks for your suggestions I'm thinking that I may need to try and achieve something less difficult at first. What about allowing the text file that I am writing to access for another process to read?

        Regards,
        suprsnipes
        Windows OS does not like this. I do not think this has changed post Vista.

        I havent done excessive testing, but things are locked unless the original process closes the file, which is what you are hitting.

        Closing and reopening the file will be a performance hit for sure.

        Are you sure you need to do this?

        Comment


          #5
          Originally posted by suprsnipes View Post
          Hi,

          I am using the following method to write to a text file and would like to know what I need to change to be able to write to the same text file without seeing 'the process cannot access the file because it is being used by another process' error message.

          Code:
          using System.IO;
           
          File.AppendAllText()
          Regards,
          suprsnipes
          According to the description on http://msdn.microsoft.com/en-us/libr...(v=vs.90).aspx, the method "opens, writes and closes" the target file. That implies that eventually, the file lock will be released.

          It seems that you will have to write a loop that uses a Try/Catch block to keep trying until it succeeds.

          Otherwise, you might try opening the file with the requisite share permissions. ref: http://msdn.microsoft.com/en-us/libr...(v=vs.90).aspx
          Last edited by koganam; 11-27-2012, 08:27 AM.

          Comment


            #6
            Also Interested

            Hello,
            I am also interested in this issue. I am trying to write in the same file from multiple sources (the same indicator in multiple time period).
            Thanks for your help.

            Comment


              #7
              Hi,

              The issue here seems to be multiple sources, and possibly concurrent access. The fastest way of synchronizing access between processes is to use Mutexes / Semaphores. Have you tried this approach?

              Comment


                #8
                Hi,

                In relation to the following reply mentioned at the bottom is a link to the Microsoft site with information on how to allow read access to a file whilst it is also being used by another process titled 'FileShare Enumeration'. How would you incorporate this into existing code that is using File.AppendAllText?

                Originally posted by koganam View Post
                According to the description on http://msdn.microsoft.com/en-us/libr...(v=vs.90).aspx, the method "opens, writes and closes" the target file. That implies that eventually, the file lock will be released.

                It seems that you will have to write a loop that uses a Try/Catch block to keep trying until it succeeds.

                Otherwise, you might try opening the file with the requisite share permissions. ref: http://msdn.microsoft.com/en-us/libr...(v=vs.90).aspx
                Regards,
                suprsnipes

                Comment


                  #9
                  Originally posted by suprsnipes View Post
                  Hi,

                  In relation to the following reply mentioned at the bottom is a link to the Microsoft site with information on how to allow read access to a file whilst it is also being used by another process titled 'FileShare Enumeration'. How would you incorporate this into existing code that is using File.AppendAllText?



                  Regards,
                  suprsnipes
                  We would have to see how you have written the code before we can say how it can be modified.

                  Comment


                    #10
                    Originally posted by koganam View Post
                    We would have to see how you have written the code before we can say how it can be modified.
                    Pretty obvious from the MS spec posted earlier. Thanks!

                    Comment


                      #11
                      Originally posted by sledge View Post
                      Pretty obvious from the MS spec posted earlier. Thanks!
                      Yes, but he wants to know how to use it in HIS code, so we need to see his code.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by cls71, Today, 04:45 AM
                      0 responses
                      1 view
                      0 likes
                      Last Post cls71
                      by cls71
                       
                      Started by mjairg, 07-20-2023, 11:57 PM
                      3 responses
                      213 views
                      1 like
                      Last Post PaulMohn  
                      Started by TheWhiteDragon, 01-21-2019, 12:44 PM
                      4 responses
                      544 views
                      0 likes
                      Last Post PaulMohn  
                      Started by GLFX005, Today, 03:23 AM
                      0 responses
                      3 views
                      0 likes
                      Last Post GLFX005
                      by GLFX005
                       
                      Started by XXtrader, Yesterday, 11:30 PM
                      2 responses
                      12 views
                      0 likes
                      Last Post XXtrader  
                      Working...
                      X