Solved

ASP.NET C# delay answer writing to file

Posted on 2016-09-21
11
57 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.
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Containers and Docker for Everyone

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

 
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
 

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

Containers and Docker for Everyone

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

707 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