Solved

ASP.NET C# delay answer writing to file

Posted on 2016-09-21
11
40 Views
Last Modified: 2016-09-30
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
0
Comment
Question by:Alex E.
  • 5
  • 5
11 Comments
 
LVL 35

Expert Comment

by:YZlat
ID: 41809052
try using Sleep() function
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 41809054
to see the change available

what do u mean by this? Where are u seeing the changes
0
 

Author Comment

by:Alex E.
ID: 41809126
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
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 41809131
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
 

Author Comment

by:Alex E.
ID: 41809140
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:Alex E.
ID: 41809141
other note if you use more each time delays more.
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 41809157
how are u calculating the time taken?
0
 

Author Comment

by:Alex E.
ID: 41809179
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
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 41809267
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
 

Author Comment

by:Alex E.
ID: 41809281
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
 
LVL 29

Accepted Solution

by:
Gautham Janardhan earned 500 total points
ID: 41809672
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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now