时差C#控制台应用程序 [英] Time difference C# console application

查看:104
本文介绍了时差C#控制台应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试创建应用。场景是我在该文件夹中有一个文件夹当一个访问/创建文件每5薄荷如果有人不能创建文件与5薄荷然后我想发送短信。发送短信是没有问题..问题是短信正在与5薄荷甚至文件创建..像有人创建文件仍然短信正在但我想要任何人无法访问文件夹与5薄荷然后5薄荷后我想发送短信。



我尝试过:



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:

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();
                       }
                   }

推荐答案

DirectoryInfo 相关内容在您的代码中似乎没用。



在您的代码中,没有代码可以发送短信,因此无法告诉您问题所在。但是,您可以很容易地在发送短信的代码上设置断点并查看发送消息的原因。


这就是说,为什么你会每4秒检查一次吗?如果你需要这样的准确性,不要使用FileSystemWatcher更合适( FileSystemWatcher Class(System.IO) [ ^ ])?



我认为你的代码有一个问题就是你添加了一个项目在表格中每隔4秒,如果您为表格中的每一行发送一条短信,您将发送大量短信。您可能需要记住是否发送了短信,如果没有短信发送,则只发送短信。否则,您将收到大量要发送的消息。



假设您向许多人发送个人消息,您可能还会溢出您的电子邮件服务器容量,具体取决于消息的方式发送。如果是这样,那么它可能会在警报条件消失后继续发送电子邮件。



另外一些行没有正确缩进,空行似乎有些随意。它可能不会影响输出,但程序员应该是一个关心细节的专业人士。



最后,似乎你不知道DRY原则(不要重复自己 - 维基百科,免费的百科全书 [ ^ ])。实际上,很容易发现 abcPath 的代码与 defPath 的代码非常相似。这对于 abcPath 所做的每一项更改都非常糟糕,您将不得不重复 defPath ,如果添加检查它的第三条路径甚至会变得更糟。



你的功能有更多的责任。你有没有读过有关SOLID原则的内容( SOLID(面向对象设计) - 维基百科,免费的百科全书 [ ^ ])。在您的情况下,您的代码不尊重SRP(单一责任原则 - 维基百科,免费的百科全书 [ ^ ])。



您的代码的另一个问题是您在代码中使用硬编码常量持续时间。如果您决定更改持续时间(例如10分钟而不是5分钟),则必须找到每次出现的硬编码值5并查看该数字是否与持续时间相关。对于大型项目,几乎不可能找到所有这些值。 4000毫秒的睡眠时间也是如此。



任何常数(除了在某些情况下像1这样的小事)都应该这样声明。例如:

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:
const int maxDelayMinutes = 5;





另外,当时间应该被认为是相同的时间,我建议你使用变量来明确意图。如果时间在调用 DateTime.Now 之间发生变化,并且代码假设时间完全相同,则可以避免一些微妙的错误。



使用调试器调试这类问题要容易得多。从像这样的问题,我们只能猜出其他代码做了什么(比如实际的短信发送),但如果问题出现在未显示的代码中,我们怎么猜呢?



另外一些确保时间正确的信息至关重要。例如,如果在代码中混合使用本地和UTC时间,则可能无法按预期工作。使用调试器很容易弄清楚这种问题。您将对目录进行修改,然后单步执行代码以查看 timediff 是否具有预期值。通常,一个可以在UTC时间工作,只使用本地时间进行显示。事实上,当夏令时变化时,该代码可能无法正常工作。



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.


这篇关于时差C#控制台应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆