a exception at writeline: ArgumentOutofRangeException:specified argument was out of range of valid values.

小姐 董 0 Reputation points
2025-01-12T12:51:43.3533333+00:00

User's image

Write line to a file by file stream "streamwriter". Whenever the variable data changes, write a line.It may run abnormally after running for several hours.It may run whole a day without abnomal. The code is as follows:

User's image

RecordRow dataRow = new RecordRow();

User's image

I can't find the cause of the abnormal. What could be the cause,Thank you

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,195 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 69,276 Reputation points
    2025-01-12T17:32:51.6+00:00

    The error suggests that parameter value of time is invalid.

    0 comments No comments

  2. Jiale Xue - MSFT 48,686 Reputation points Microsoft Vendor
    2025-01-13T03:17:36.7666667+00:00

    Hi @小姐 董 , Welcome to Microsoft Q&A,

    This error may be caused by intraoperative possibility, buffer overflow or management problem, data anomaly, concurrent access violation, and incorrect resource release.

    Before writing to the file, check whether the value of each dataRow field is valid. For example:

    if (double.IsNaN(dataRow.XTorque) || double.IsInfinity(dataRow.XTorque))
    {
    dataRow.XTorque = 0.0; // or other value
    }
    
    

    Perform similar checks on all numeric fields to ensure that the data is valid.

    Call the Flush method regularly to clear the buffer and prevent buffer overflow:

    if (sw != null)
    {
    sw.WriteLine(...); // Write a line of data
    if (lineCounter % 1000 == 0) // Flush every 1000 lines written
    {
    sw.Flush();
    }
    }
    

    Add a try-catch block in the write operation to capture and log exception information:

    try
    {
    sw.WriteLine(...);
    }
    catch (Exception ex)
    {
    Console.WriteLine($"Error occurred when writing to the file: {ex.Message}");
    }
    

    To ensure that the StreamWriter is properly released after use, you can use the using statement:

    using (StreamWriter sw = new StreamWriter("filePath", true))
    {
    sw.WriteLine(...);
    }
    

    If multiple threads are writing to a file at the same time, use lock to ensure that the write operation is thread-safe:

    
    private static readonly object fileLock = new object();
    
    lock (fileLock)
    {
    sw.WriteLine(...);
    }
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.