2

I'm using Filesystemwatcher to pick up a file, convert this to UTF-8 and transfer this to the destination. Now what I want to achieve is I need to delay the xml being written to the destination. i.e. by a mere 15-20secs. I know I can use the following here:

System.Threading.Thread.Sleep(milliseconds);

But if I do this, it would delay the thread completely won't it. What would happen to the filesystemwatcher, would it stop picking up the files. My aim is to delay but don't miss any files that would be changed in the source folder. Here is how I'm doing it for the moment:

var doc = new XmlDocument();
doc.Load(FileName);
XmlWriterSettings settings = new XmlWriterSettings { Encoding = Encoding.UTF8, Indent = true };
using (var writer = XmlWriter.Create(destinationFile, settings))
{
    System.Threading.Thread.Sleep(15000);
    doc.Save(writer);
}

This would stop the thread and delay writing to the xml file which is what I want. But what would happen to the file system watcher then, will it stop as well - as it's part of the same thread.

Mo Patel
  • 2,321
  • 4
  • 22
  • 37
user726720
  • 1,127
  • 7
  • 25
  • 59
  • Delay in a new thread: http://stackoverflow.com/questions/6913589/c-sharp-delaying-a-thread – L-Four Apr 11 '13 at 09:34
  • You have to use [Thread](http://msdn.microsoft.com/en-us/library/aa645740(v=vs.71).aspx): create one when `FilesystemWatcher` find new file, start it, forget about it.. in the thread do `Thread.Sleep(15000);` and only then create new file and save it. – Sinatr Apr 11 '13 at 09:34
  • `Task.Delay(15000).ContinueWith(_ => doc.Save(writer));` ??? – I4V Apr 11 '13 at 09:39
  • @I4V: Should this only delay the saving of the file and not the filesystemwatcher – user726720 Apr 11 '13 at 09:41
  • 1
    @user726720 and it does so. Put `Task.Delay(5000).ContinueWith(_ => MessageBox.Show("aaa"));` to form_load and see how it works.. – I4V Apr 11 '13 at 09:42
  • @I4V: It says 'Task' doesnot exist in the current context. What namespace assembly is this using – user726720 Apr 11 '13 at 09:47
  • @user726720 `System.Threading.Tasks` – I4V Apr 11 '13 at 09:47
  • @I4V: SOrry mate, I'm just new to this assembly. I have put that, and now it say: 'System.Threading.Tasks.Task' does not contain a definition for 'Delay' – user726720 Apr 11 '13 at 09:51
  • You need .NET Framework 4.5 for Task. – L-Four Apr 11 '13 at 09:53
  • Delay is available in .Net 4.5 if use use a lower version, you can use the `Delay` method [here](http://stackoverflow.com/a/13058866/932418) – I4V Apr 11 '13 at 09:53
  • @L-Three Only `Delay` – I4V Apr 11 '13 at 09:54
  • What do you mean? What version are you using? You need 4.5 to use it. – L-Four Apr 11 '13 at 09:55
  • Sure, let me download and try – user726720 Apr 11 '13 at 09:59
  • I guess that doesn't support Windows Xp, so maybe not an option for me but +1 for the answer and I will keep that in mind when I upgrade to windows 7 – user726720 Apr 11 '13 at 10:03

3 Answers3

2

You can use System.Threading.Timer class

var doc = new XmlDocument();
doc.Load(FileName);
XmlWriterSettings settings = new XmlWriterSettings { Encoding = Encoding.UTF8, Indent = true };

new System.Threading.Timer((_) =>
    {
        using (var writer = XmlWriter.Create(destinationFile, settings))
        {
            doc.Save(writer);
        }
    })
.Change(15000, -1);
I4V
  • 34,891
  • 6
  • 67
  • 79
1

create a thread for the stuff you want to delay and a separate thread for the stuff you don't want to delay

Edit Try this

System.Threading.Thread newThread;
newThread = new System.Threading.Thread(anObject.AMethod); // one to delay

System.Threading.Thread newThread2;
newThread2 = new System.Threading.Thread(anObject.AMethod); //one not to delay

Then add the Threadname.Start(); to the start of the procedure that you want to delay, and the other on the one that you don't want to delay. Then delay the one you want to delay using

System.Threading.Thread.Sleep(milliseconds);

like you did before

Hope this helps

06needhamt
  • 1,555
  • 2
  • 19
  • 38
0

What about using a BlockingCollection?
It is available on 4.0.
The Filesystemwatcher is the producer and the consumer is the xmlwrite.
You can sleep in the consumer and not effect the producer.

BlockingCollection Class

paparazzo
  • 44,497
  • 23
  • 105
  • 176