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 SalmaTrader, 07-07-2026, 10:26 PM
    0 responses
    52 views
    0 likes
    Last Post SalmaTrader  
    Started by CarlTrading, 07-05-2026, 01:16 PM
    0 responses
    25 views
    0 likes
    Last Post CarlTrading  
    Started by CaptainJack, 06-17-2026, 10:32 AM
    0 responses
    16 views
    0 likes
    Last Post CaptainJack  
    Started by kinfxhk, 06-17-2026, 04:15 AM
    0 responses
    23 views
    0 likes
    Last Post kinfxhk
    by kinfxhk
     
    Started by kinfxhk, 06-17-2026, 04:06 AM
    0 responses
    24 views
    0 likes
    Last Post kinfxhk
    by kinfxhk
     
    Working...
    X