質問

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).

enter link description here

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.

役に立ちましたか?

解決

You can't put a method within a method like that, or event handler within an event handler, such as it is; you will need to define the FileSystemWatcher handler alongside the timer handler. But your watcher will do the ticking-over style monitoring for you, and raise the event when that occurs according to it, and not your timer.

To explain a little, it would make sense to put it like this, and you don't have that particular error (or set of errors) any more:

private void timer1_tick (object sender, EventArgs xz) {

}

void SysWatch_Created(object Sender, FileSystemEventArgs w) {
  MessageBox.Show("File " + w.FullPath + "Creato");
}

However, then you still need to call it at the interval, and calling the handler method manually doesn't make much sense here. Consider the following:

private void timer1_tick (object sender, EventArgs xz) {
  SysWatch_Created(this, null);
}

The sender might be expected to be a FileSystemWatcher, but not necessarily so that's of the least concern: otherwise, what do you expect to pass for where I place null in this instance? Only the watcher has this information in this context.

Back to the point. You don't need a timer, FileSystemWatcher will do the polling internally and inform you, because you tell it to (Created += new FileSystemEventHandler(SysWatch_Created);). You can't specifically set the polling interval of this thing, though.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top