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 CarlTrading, 03-31-2026, 09:41 PM
    1 response
    81 views
    1 like
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 04-01-2026, 02:41 AM
    0 responses
    42 views
    0 likes
    Last Post CarlTrading  
    Started by CaptainJack, 03-31-2026, 11:44 PM
    0 responses
    64 views
    2 likes
    Last Post CaptainJack  
    Started by CarlTrading, 03-30-2026, 11:51 AM
    0 responses
    68 views
    0 likes
    Last Post CarlTrading  
    Started by CarlTrading, 03-30-2026, 11:48 AM
    0 responses
    55 views
    0 likes
    Last Post CarlTrading  
    Working...
    X