Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Error Msg : the process cannot access the file used by another process

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

    Error Msg : the process cannot access the file used by another process

    Hello NT Team,

    I have a CSV file which looks something like this :
    Entry Price Quantity SL Level TP Level Side
    nothing nothing nothing nothing nothing
    As you can see, using the code below, I try to make sure if I have the file already, otherwise I create one with the same content above :

    Code:
    if (!File.Exists(path))
    {[INDENT]sw = File.AppendText(path);
    
    if (writeHeaders) {
    sw.WriteLine("Entry Price, " + "Quantity, " + "SL Level, " + "TP Level, " + "Side");
    writeHeaders = false;[/INDENT]
     
     }
    
    sw.WriteLine("nothing" + "," + "nothing" + "," + "nothing" + "," + "nothing" + "," + "nothing");
    sw.Close();
    
    }
    but the if a position is taking place, I want to replace "nothing" with real values, depends on each column :

    Code:
    if (File.Exists(path) && PositionAccount.Quantity > 0)
    {[INDENT]List<string> newLines = new List<string>();
    string[] existingLines = File.ReadAllLines(path, encode);
    
    foreach (string line in existingLines)
    {[/INDENT][INDENT=2]string[] columns = line.Split(new char[] {','});
    
    string EntryPrice = columns[0];
    string Qtty = columns[1];
    string SSLevel = columns[2];
    string TTPLevel = columns[3];
    string PSide = columns[4];
    
    EntryPrice = EntryPrice.Replace("nothing", PositionAccount.AveragePrice.ToString());
    Qtty = Qtty.Replace("nothing", QtyMini.ToString());
    SSLevel = SSLevel.Replace("nothing", LongSL.ToString());
    TTPLevel = TTPLevel.Replace("nothing", LongTP.ToString());
    PSide = PSide.Replace("nothing", "Long");
    
    columns[0] = EntryPrice;
    columns[1] = Qtty;
    columns[2] = SSLevel;
    columns[3] = TTPLevel;
    columns[4] = PSide;
    
    string newLine = string.Join(",", columns);
    newLines.Add(newLine);[/INDENT][INDENT]}
    
    File.WriteAllLines(path, newLines);[/INDENT]
     
     }
    both above codes are working perfectly, and doing what they are supposed to do, EXCEPT THAT, when I try to READ the csv file from another strategy, it shows an Errors Message of "Error Msg : the process cannot access the file used by another process" and it disables everything.

    I really tried all possible ways that I came across in all forums to fix this problem, but I was not able to do it, I think that the file is not closed from the first opening this is why it shows this message, so I tried using (), I tried FileSharing, File Access, I tried FileStream, I tried StreamReader, but in some of them I was not able to readalllines and writealllines, as well I was not able to read only line[1] for example, and modify it and replace it.

    Code:
    using (mystream = File.Open(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
    {
    
    }
    Can you plz tell me how to fix it ? Thank you
    Last edited by MohammedAmine; 01-04-2022, 12:26 PM.

    #2
    Hello MohammedAmine,

    What you are doing it would be expected to run into that error, multiple scripts cannot reliably access the same file due to timing. You could potentially surround the code with a try/catch and in case of error set a bool to true so you know you need to retry the file at a later time. There is not a specific suggestion that I could make for sharing data between two strategies, you could research using a C# static class for that purpose however that is outside of the scope that our support can assist. static is a C# concept.


    I look forward to being of further assistance.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by NullPointStrategies, Yesterday, 05:17 AM
    0 responses
    62 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    134 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    75 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    45 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    50 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X