locked
IIS worker process locking file RRS feed

  • Question

  • User64294366 posted

    Hello - I have a function that writes a file to a directory using Streamwriter. The problem I have is that the file is locked by IIS and not release unless the browser is closed. Or in the case of Visual Studio IIS Express is stopped. Streamwriter is contained in a using statement and I am closing and disposing but still the file is locked. Does anyone have a suggestion? Thanks

     public static void WriteDataToFile(DataTable dt, string name)
        {
            int i = 0;
            int rowCount = dt.Rows.Count;
            int colCount = dt.Columns.Count;
           
    
            string path = ConfigurationManager.AppSettings["Merged"].ToString();
    
               using (StreamWriter sw = new StreamWriter(@path + "\\" + name + ".rki", false))
                {
    
                    Char c = new Char();
    
                    c = (char)(28);
    
    
                    foreach (DataRow row in dt.Rows)
                    {
                        object[] array = row.ItemArray;
    
                        for (i = 0; i < array.Length - 3; i++)
                        {
                            sw.Write(array[i].ToString() + c);
                        }
                        sw.Write(array[i].ToString());
                        sw.WriteLine();
    
                    }
    
                    sw.Close();
                    sw.Dispose();
                     
    
                }
            }

    Thursday, October 16, 2014 2:05 PM

Answers

  • User64294366 posted

    I put a 1 second delay with thread.sleep(1000) after closing and disposing. That allowed time for the process to be dropped.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 16, 2014 2:48 PM

All replies

  • User64294366 posted

    I put a 1 second delay with thread.sleep(1000) after closing and disposing. That allowed time for the process to be dropped.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 16, 2014 2:48 PM
  • User-1956403626 posted

    using (StreamWriter sw = new StreamWriter(@path + "\\" + name + ".rki", false))
                {

    you are trying to write file concurrently. Each asp.net thread from incoming request is trying to access the file.

    Thread.Sleep will defer incoming threads by 1 second. Things might get messed up if it took more than a second.

    These could help you out.

    http://stackoverflow.com/questions/3507770/write-to-a-file-from-multiple-threads-asynchronously-c-sharp

    http://forums.asp.net/t/1719966.aspx?Write+Text+file+Concurrently

    Personally, I wud store MD5 or hash of incoming dt and compare it next time before saving it to file.

     

     public static void WriteDataToFile(DataTable dt, string name)

    For reasonably incoming requests I've found high I\O to be blocking.

    Monday, October 20, 2014 7:48 AM