Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

File I/O access error in OnOrderUpdate event

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

    File I/O access error in OnOrderUpdate event

    Hi,

    I have a function that registers updates of an active order using the OnOrderUpdate event. It logs this information to a text file.

    From time to time, the system gives me an I/O access error -> "The process cannot access the file XXXXX because it is being used by another process". How can I fix this?

    Thanks​

    Code:
    private void OnOrderUpdate(object sender, OrderEventArgs e)
    {
        Order order = e.Order;
    
        registerOrderUpdate(order);
    }
    
    void registerOrderUpdate(Order order)
    {
        // Crear la cabecera del CSV si el archivo no existe
        if (!File.Exists(filePath))
        {
            string header = getHeader ();
            File.WriteAllText(filePath, header);
        }
    
        string csvLine = getCSVLine(order);
    
        // Añadir la línea al archivo CSV
        try
        {
            // Añadir la línea al archivo CSV
            File.AppendAllText(filePath, csvLine);
        }
        catch (UnauthorizedAccessException ex)
        {
            // Manejo de errores específicos de permisos o accesos denegados
            Print($"Error de acceso al archivo: {ex.Message}");
        }
        catch (IOException ex)
        {
            // Manejo de errores específicos de E/S (input/output)
            Print($"Error de I/O al escribir en el archivo: {ex.Message}");
        }
        catch (Exception ex)
        {
            // Manejo genérico para cualquier otro tipo de excepción
            Print($"Ocurrió un error inesperado al escribir en el archivo: {ex.Message}");
        }        
    }​

    #2
    Hello Powerbucker,

    Likely the file is still open and being accessed.

    You could choose to use a timer to try writing the file again in 1 second.

    You could choose to write to a different file with a different file name.

    You could choose to accumulate the information to a string variable, and then write the entire file all at once one time.
    Chelsea B.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    556 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    324 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    101 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    545 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    547 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X