Link to home
Start Free TrialLog in
Avatar of Alex E.
Alex E.

asked on

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
Avatar of YZlat
YZlat
Flag of United States of America image

try using Sleep() function
Avatar of Gautham Janardhan
Gautham Janardhan

to see the change available

what do u mean by this? Where are u seeing the changes
Avatar of Alex E.

ASKER

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.
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?
Avatar of Alex E.

ASKER

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.
Avatar of Alex E.

ASKER

other note if you use more each time delays more.
how are u calculating the time taken?
Avatar of Alex E.

ASKER

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.
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.
Avatar of Alex E.

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Gautham Janardhan
Gautham Janardhan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial