ASP.NET C# delay answer writing to file

With this code:

        private void button1_Click(object sender, EventArgs e)
        {
            lineChanger("QWERTY", "MyTextFile.txt", 2);
        }

        static void lineChanger(string newText, string fileName, int line_to_edit)
        {
            int attemptCounter = 0;
            for (attemptCounter = 1; attemptCounter <= 10; attemptCounter++)
            {
                try
                {
                    System.Diagnostics.Debug.WriteLine("Attempt " + (attemptCounter));
                    string[] arrLine = File.ReadAllLines(fileName);
                    arrLine[line_to_edit - 1] = newText;
                    File.WriteAllLines(fileName, arrLine);
                    HttpContext.Current.Session["chkbug"] = "sysworking";
                    // No error - break
                    break;
                }
                catch (IOException ioex)
                {
                    System.Diagnostics.Debug.WriteLine("IOException happened: " + ioex.ToString());
                    if (attemptCounter >= 10)
                    {
                       HttpContext.Current.Session["chkbug"] = "sysnotworking";
                    }
                }
            }
        }

Open in new window


If we execute via locally in a 127.0.0.1 with default ASP.NET 4 pool works perfect we can click the button anytime for example each second or each 20 seconds no problem at all the change at the moment. However we placed the application in a real server www with a real domain and now in that server (other different hardware) if you execute the application is ok the first change is immediately but you need to wait more than 3 minutes to see the change available. Is like if a queue or something makes the delay to make the change immediately when you wish.

What could be the problem or what can we do to make the changes immediately without a queue or delay?

Thank you
Alex E.Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Gautham JanardhanConnect With a Mentor Commented:
can you add some logging and see how many times this loop is running

            for (attemptCounter = 1; attemptCounter <= 10; attemptCounter++)

logging will also help u figure out what is taking time

http://www.codeproject.com/Articles/8245/A-Brief-Introduction-to-the-log-net-logging-librar
0
 
YZlatCommented:
try using Sleep() function
0
 
Gautham JanardhanCommented:
to see the change available

what do u mean by this? Where are u seeing the changes
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
Alex E.Author Commented:
that script is to replace a existing line of a text file when you call like this:

lineChanger("QWERTY", "MyTextFile.txt", 2);

Open in new window


We use that function to replace lines of a text file when the user click some options in a form in our applciation and the delays are when the user click that buttons to make the changes to the .txt files. When the user click one of that buttons the application pass to command "lineChanger"  and then to "File.WriteAllLines" like the script on the main post shows and there is when the delays occur. We are testing more and sometimes takes 15 minutes for the server to modify the line to be replaced in the txt file sometimes less. At the end the server makes the change to the text file but we don´t know where are happening that delays if from IIs or from other area. And like I mentioned before calling via 127.0.0.1 via locally runs amazing perfect is immediately the response no delays

We can´t put the sleep() function because at the end the system ALWAYS makes the change to the .txt file and if we use sleep()  could stop the process really and there is no need to do that because the server delayed but finish the writing to the txt file. We just want to short the time of that delays when writing to the txt files with the "File.WriteAllLines"

What could be the problems of delays when the application try to write to the txt files. Like i wrote before sometimes take 15 minutes to be reflected the change in the txt file is so long and that time we note varies.
0
 
Gautham JanardhanCommented:
What i was asking was how do u verify that changes to the file was done.. Do u have a page which downloads the file? or do u go into the server and chk that the file was changed manually?
0
 
Alex E.Author Commented:
Yes the changes are done like I mentioned before pretty well. Delayed but all done perfectly changed  in the txt file and for example if you click so many buttons to make changes all changes will be made but all are delayed is like something put in queue or something and the server does everything but slow.
0
 
Alex E.Author Commented:
other note if you use more each time delays more.
0
 
Gautham JanardhanCommented:
how are u calculating the time taken?
0
 
Alex E.Author Commented:
To calculate time we have opened a text editor with the txt file to modify and when the file is modified the text editor detect the change and tell us a pop up with the note if we want to reload the file because is detected a change in the txt file and if we click reload in that text editor the changes are made perfectly.
0
 
Gautham JanardhanCommented:
ok you can u try this.. It might be that the text editor might be taking time to load the changes..

keep the text editor closed. push the button to make the change.. open the file and see if the changes are done.

Also if you are doing this on a remote machine you might want to check the files directly on the server.
0
 
Alex E.Author Commented:
All the files are in the same server the form just take from client the status of the button if clicked or not and then all the replacement is done in the server side.  Just the application is running via Internet and called via Internet.

We closed the text editor and opened the txt file an there is no change immediately continue with delays.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.