peter
peter

Reputation: 8662

How to execute a method one time in ASP.net

I have web form which has a button.When you click that button,it will create a text file and write something to it.Just imagine like i am writing large things of 1G content ,which will change once in a day.And this is an asp.net application and many users will use.So suppose first user clicks at morning 6.o clock it will generate .Now i want to resuse it for others rather creating a new one till next morning 6 o clock.How to do it.I am posting a small prototype code

try
{
     File.WriteAllText("E:\\test.txt", "welcome");
}
catch (Exception ex)
{
      Response.Write(ex.Message);
}

NB:This is an asp.net application so cant think of thread.So i am not thinking

While(true)
{
   Thread.Sleep()  etc
}

Upvotes: 2

Views: 410

Answers (4)

Jaydip Jadhav
Jaydip Jadhav

Reputation: 12309

Use File.GetLastWriteTime Method to check last modification in file

try
{
   if(!File.Exists("E:\\test.txt") )
   {
     File.WriteAllText("E:\\test.txt", "welcome");     
   } 
   else
   { 
       if(File.GetLastWriteTime(path).Day != DateTime.Now.Day)   
       {
         //code for next day
       }  
   }

}
catch (Exception ex)
{
  Response.Write(ex.Message);
}

Upvotes: 2

Ceres
Ceres

Reputation: 3648

This should prevent two or more threads from writing the same file twice.

The first thread to grab the lock will create the file, then the other threads will skip creating the file with the second check of the file inside the lock.

public static object fileLock = new object();

public void createFile()
{

    if (File.Exists("filepath") == false) {

        lock (fileLock) {

            if (File.Exists("filepath") == false) {
                  File.WriteAllText("E:\\test.txt", "welcome");
            }

        }

    }
}

Upvotes: 1

Michael A. Allen
Michael A. Allen

Reputation: 627

Perhaps you should try using an Application variable to store the last time the file has been written ( a date value ) and just be sure that the file is only ever written once per day. For example:

Dim dt as DateTime
If TryCast(Application("LastFileWrite"), dt) Then
    If String.Compare(dt.Date.ToString(), Now.Date.ToString()) <> 0 Then
        ' we're a different day today, go ahead and write file here
    End If
Else
    ' we've never writting this application variable, this is
    ' the first run, go ahead and write file here as well
End If

For more information about the Application state, take a look at the following documentation:

https://msdn.microsoft.com/en-us/library/bf9xhdz4(v=vs.71).aspx

Upvotes: 1

Seano666
Seano666

Reputation: 2238

Assuming you are making a new file every day, and already have delete logic in place at the end of the day. Check and see if the file exists before you create it.

try
        {
            if (//file does not exist)
               File.WriteAllText("E:\\test.txt", "welcome");
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }

You could also check on the date of the file and if outside of your parameters then delete and create a new one (in the same if condition as the 'exists' logic).

Upvotes: 1

Related Questions