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 DannyP96, 05-18-2026, 02:38 PM
    1 response
    85 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 05-11-2026, 05:56 AM
    0 responses
    143 views
    0 likes
    Last Post CarlTrading  
    Started by CarlTrading, 05-10-2026, 08:12 PM
    0 responses
    83 views
    0 likes
    Last Post CarlTrading  
    Started by Hwop38, 05-04-2026, 07:02 PM
    0 responses
    256 views
    0 likes
    Last Post Hwop38
    by Hwop38
     
    Started by Mindset, 04-21-2026, 06:46 AM
    0 responses
    334 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Working...
    X