0

When one or more files are added to a folder "SentBox", my app will detect it and proceed them ONE BY ONE, here is my code:

public void SetUp_Timers()
{

    MsgHandler.Interval = new TimeSpan(0, 0, 1);
    MsgHandler.Tick += MsgHandler_Tick;
    MsgHandler.Start();
}

private void MsgHandler_Tick(object sender, EventArgs e)
{

    if (Directory.GetFiles(Path_SentBox).Count() > 0)
    {
        string pt = Directory.GetFiles(Path_SentBox)[0];
        Message ms = JsonConvert.DeserializeObject<Message>(Security_ReadAllText(pt));
        ProceedMsg(ms, pt);
    }
}

public async void ProceedMsg(Message msg, string msgpath)
{

    MsgHandler.Stop();

    //more code....

    File.Delete(msgpath);
    MsgHandler.Start();
}

So far, this never gives me an unexpected result. Recently, I found a class called FileSystemWatcher. Based on what they say here and here, the class is used to watch for changes in a specified directory.

My questions:

  1. Is it possible to achieve my goal with FileSystemWatcher ?
  2. If so, which one is better?
  3. And, is there a better way than these two?

EDIT:

I'm not sure if you need this:

public void SendMessage(Message msg)
{

    string fn = msg.Type + "_" + FileNameFix(DateTime.Now.ToShortDateString() + DateTime.Now.ToLongTimeString() + Path.GetRandomFileName().Replace(".", "") + ".kmsg");
    Security_WriteAllText(Path_SentBox + "\\" + fn, JsonConvert.SerializeObject(msg));
}
Community
  • 1
  • 1
Wahyu
  • 4,203
  • 1
  • 19
  • 21
  • There is a comprehensive example at MSDN for the [FileSystemWatcher Class]](https://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx). Since it's part of the framework, I would go with that rather than trying to roll your own implementation. Here is the actual [code example](https://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx#Anchor_7). – Tim Apr 18 '16 at 19:40
  • @Tim based on the [code sample](https://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx#Anchor_7), what if a file (or more) is added to the folder while the current OnChanged is not finished yet, will it raise another OnChanged asynchronously? Also it says "Keep your event handling code as short as possible.". My handling code may proceed `Message` less than a sec, but in some case, more than a hour. – Wahyu Apr 18 '16 at 19:57
  • Each event should be a discrete, distinct run of the code, so I don't see a problem with several files being added in a short period of time. As far as keeping things short, you might consider having the event itself kick of a longer-running process, either in the background in a fire-and-forget manner or possibly async. – Tim Apr 18 '16 at 20:18
  • @Tim hmm, you're right. I will try to achieve my goal with the class. But, since there is a ProgressBar in my app, i should create another Progressbar for each additional event, right? This would be a hard work (to me), so i have a question. Does my implementation really that bad in performance term ? If its not, i think i will do the hard work later since there is a lot of things i have to do rather than improving my current implementation. – Wahyu Apr 18 '16 at 20:52
  • 1
    If it works, and there's no serious issues with it, and you have more pressing things to attend to, then I'd say leave it be. In other words, if its not broke, don't fix it :) You can always circle back to it for performance improvements/rewrites when you have time, or if it does become an issue. – Tim Apr 18 '16 at 21:04

0 Answers0