Click here to Skip to main content
15,890,897 members
Please Sign up or sign in to vote.
1.35/5 (5 votes)
I try to create app . scenario is i have a folder in that folder when some one access / create file every 5 mint if anyone can not create file with in 5 mint then i want to send sms. sending sms is not problem .. problem is sms is going with in 5 mint even file is created .. like someone create files and still sms is going but i want when anyone can not access folder with in 5 mint then after 5 mint i want to send sms .

What I have tried:

C#
public static void Main()
       {

           while (true)
           {
               try
               {


                   string abcPath,

                   abcPath= ConfigurationManager.AppSettings.Get("abcPath");


                   DirectoryInfo dir = new DirectoryInfo(abcPath);
                   dir.Refresh();


                   DateTime abc = File.GetLastAccessTime(abcPath);


             Console.WriteLine("the  time for  abc {0}", abc );


                   TimeSpan timediff = DateTime.Now - abc ;




                   if( timediff.TotalMinutes > 5)
                   {
                       Dt db = new Dt();
                       var  u = db.tblurgent;
                       foreach (var a in u)
                       {
                           tbl_OutBox tb = new tbl_OutBox();
                           tb.FromSIM_No = a.SimNo;
                           tb.ToSIM_No = a.SimNo;
                           tb.ToText = "Check abc";
                           tb.Reply = "NA";
                           tb.Response = "NA";
                           tb.RegNo = "NA";
                           tb.Datetd = DateTime.Now;
                           tb.FFID = "NA";
                           tb.UserId = "You";
                           tb.FromText = "Check abc";
                           db.tbl_OutBoxes.InsertOnSubmit(tb);
                           db.SubmitChanges();
                       }
                   }
Posted
Updated 3-Jul-16 19:55pm
v2
Comments
Philippe Mori 1-Jul-16 10:07am    
It is totally useless to show code for both abc and def as it won't help us to solve your problem. It is up to you to give minimal code required to reproduce the problem.

1 solution

DirectoryInfo related stuff seems useless in your code.

In your code, there is no code that send SMS so it is not possible to tell you where the problem is. However, it would be very easy for you to put a breakpoint on the code that does send the SMS and see why a message is sent.

That being said, why would you check every 4 seconds? If you need such accuracy, would not using a FileSystemWatcher be more appropriate (FileSystemWatcher Class (System.IO)[^])?

I think that one problem with your code is that you add a item in the table every 4 seconds so you would send a lot of SMS if you send one for each row in the table. You probably need to remember if a SMS was sent and only sent one if no SMS where sent. Otherwise, you would have a lot of message to send.

Assuming that you send individual message to many people, you might also overflow your email server capacity depending how messages are sent. If so, then it might continue to send email long after the alert condition is gone.

Also some lines are not properly indented and blank lines seems somewhat arbitrary. It might not affect the output but a programmer should be a professional who care for details.

Finally, it seems that you don't know the DRY principle (Don't repeat yourself - Wikipedia, the free encyclopedia[^]). In fact, it is very easy to spot that the code for abcPath is very similar to the code for defPath. This is very bad as every changes you make for abcPath, you will have to repeat it for defPath and if you add a third path to check it would even get worst.

You function has more that one responsability. Have you ever read about SOLID principles (SOLID (object-oriented design) - Wikipedia, the free encyclopedia[^]). In your case, your code does not respect the SRP (Single responsibility principle - Wikipedia, the free encyclopedia[^]).

Another problem with your code is that you use hard-coded constant in your code for the duration. If you decide to change the duration (say 10 minutes instead of 5), you have to find each occurrence of hard-coded value 5 and see if that number is related to the duration. For large projects, it would be almost impossible to find all such values. Same thing for the 4000 ms of sleep time.

Any constant (except trivial case like 1 in some circumstances) should be declared as such. For example:
C#
const int maxDelayMinutes = 5;


Also, when a time should be considered the same time, I would suggest you to use a variable to make the intent clear. It could avoid some subtle bugs if the time change between call to DateTime.Now and the code assume that time is exactly the same.

That kind of problem is much easier to debug using a debugger. From a question like this one, we can only guess what other code do (like the actual SMS sending) but if the problem is in the code not shown, how can we guess it?

Also some information like ensuring that time is correct is crucial. For example, if you mix local and UTC time in your code, then it might not work as intended. That kind of problem is very easy to figure out with a debugger. You would make a modification to the directory and then step in your code to see if timediff has the expected value. Usually, one would works in UTC time and only use local time for display purpose. In fact, that code will probably not work as expected when daylight saving time changes.
 
Share this answer
 
Comments
Leo Chapiro 30-Jun-16 10:12am    
A very good and IMHO helpful answer! +5
super_user 1-Jul-16 0:31am    
actually msg is sent from database. here i just insert the row in table and from that table msg is sending.. when i set break point i see that 5 mint not over and data is sending to table ... how i modified the code? @Philippe Mori
super_user 1-Jul-16 1:21am    
In your code, there is no code that send SMS so it is not possible to tell you where the problem is. However, it would be very easy for you to put a breakpoint on the code that does send the SMS and see why a message is sent.
------sms is sending from database so forget about sms sending--------


That being said, why would you check every 4 seconds? If you need such accuracy, would not using a FileSystemWatcher be more appropriate (FileSystemWatcher Class (System.IO)[^])?
-----yes i need that such accuracy how i do that. i want to check every 5 mint-------


I think that one problem with your code is that you add a item in the table every 4 seconds so you would send a lot of SMS if you send one for each row in the table. You probably need to remember if a SMS was sent and only sent one if no SMS where sent. Otherwise, you would have a lot of message to send.
----yes that is the problem sms sending continuously .. when i check my cell phone there is about 12+ sms .. where as i want when file is not created with in 5 seconds then want to insert record in table only one time ---


and i set path address in app.config file i.e.C:\Users\Administrator\Desktop\abc
Philippe Mori 1-Jul-16 10:33am    
Somehow, you need to have information in the database to know if a change occurs after last sent SMS. Or you might keep the information in memory if you don't care much about the effect of restarting the console application.

Thus if a change is detected at some point, you need to update information so you don't send SMS after that. You probably also need to remember if a SMS was sent.

Given that you don't want to tell us how SMS are sent, then we cannot help you more precisely that giving some general ideas.
super_user 1-Jul-16 2:29am    
i can not get last access file of folder abc and def. where as today is 1-07-2016 when i set breakpoint i got date 6/30/2016

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900