I need to check a folder for some files of a certain type, then add them to an array, and print and delete them one by one. If a new file is added during the process, it will just get added to the printing queque. I'm trying to monitor if a file is created every 1 second. To make this I'm using a FileSystemWatcher with a Timer. I'm trying to binding the 2 event functions togheter but I get weird errors when trying. I'm doing this in a Form application, and I saw those 2 namespaces for the first time today, and I know nothing about multithreading, background threads or difference between System.Threading.Timer an System.Windows.Forms.Timer and which one is best for this particular situation, so I might need some quick clarification about this too, if the problem gets too specific on these subjects.
Basically the code is this one (it's part of a bigger program, so I'll try to just stick the problem related code, also, don't mind the extra using namespace which are part of the whole project. Finally I don't know how to highlight the C# syntax in the codeblocks here and the formatting tool with ctrl+K made up what you're reading in the next code block. I tried to indent it a bit for a better reading.):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
using System.IO.Ports;
using System.Diagnostics;
private Timer timer1; //Windows.Form Timer
private void button1_Click(object sender, EventArgs e) {
try {
InitTimer();
FileSystemWatcher SysWatch = new FileSystemWatcher(FilesPath, "*.*");
SysWatch.Created += new FileSystemEventHandler(SysWatch_Created);
SysWatch.EnableRaisingEvents = true;
}
catch (Exception exc) {
MessageBox.Show(exc.Message);
}
}
private void InitTimer() {
timer1 = new Timer();
timer1.Tick+=new EventHandler(timer1_Tick);
timer1.Interval = 2000;
timer1.Start();
}
private void timer1_tick (object sender, EventArgs xz) {
void SysWatch_Created(object Sender, FileSystemEventArgs w) {
MessageBox.Show("File " + w.FullPath + "Creato");
}
}
What I was trying to do is trying to nest the file creation event controller inside the timer so it checks the file creation, but probably there is something wrong in the way I'm doing it.
I get this kind of stuff (Just when I try to nest SysWatch_Created into timer1_Tick, until then all was working perfect).
I think it has something to do with the fact that I'm trying to nest 2 methods with Event Args or something... Don't know
Any example on how to do it better is welcome.
Thanks all.