首先,我正在开发一个相当高级别的文件系统,我需要能够(非常精确定位)及时识别和处理文件。话虽如此,我正在使用一个将使用FileSystemWatcher的系统。然而,观察者的整个问题是当涉及大文件时抛出事件时往往会出现问题。
为了解决这个问题,我正在研究一个抽象类,它可以在文件系统观察者创建文件后单独处理这些文件。
我遇到的当前路障是我对文件的进程外验证正在抛出一个事件,我只是遇到了问题。
public abstract class absFile
{
public delegate void FileAvailable(object sender, FileEventArgs e);
public event FileAvailable OnFileAvailable;
public void isAvailable()
{
// Create a new threaded instance of the AvailableCheck private void
// This method will be run out of process to allow other operations to continue.
Thread toAvailableCheck = new Thread(new ThreadStart(AvailableCheck));
// Once the threaded object is created, start it.
toAvailableCheck.Start();
}
private void AvailableCheck()
{
// Declaring the file stream toThisFile to be used later with the File.Open
FileStream toThisFile;
// Declaring and instantiating the attempt counter for the loop
int tiAttemptNumber = 0;
// Declaring the event args for returning the events that are
// used by this object.
FileEventArgs toFileEventArgs = new FileEventArgs();
do {
try
{
// Attempt to open the file. If this fails the try
// will interrupt the processing and it will be caught.
toThisFile = File.Open(this.FilePath, FileMode.Open);
// If the file.open method does not fail, the isFileAvailable
// property will be set to true by updating the mbIsAvailable
// private boolean.
mbIsAvailable = true;
// populate the file event args to send back
// the number of attempts made at the file and the pause lenght
toFileEventArgs.Attempts = tiAttemptNumber;
toFileEventArgs.Pause = this.AttemptPause / 1000;
// This event is called when the file is complete.
// The client application will be able to handle this event.
OnFileAvailable(this, toFileEventArgs);
// close and dispose of the filestream.
toThisFile.Close();
toThisFile.Dispose();
}
catch (Exception toException)
{
// Since the open failed, add 1 to the counter so that
// it will eventually time out.
tiAttemptNumber++;
// Set the isFileAvailable property to false. This property
// will default as false, but as a part of standard, make sure that
// if the open fails that the flag IS set to false by updating the
// mbIsAvailable private boolean.
mbIsAvailable = false;
// Put the thread to sleep for the ammount of time specified
// by the AttemptPause. This will give the file time to finish
// whatever process it is involved in.
Thread.Sleep(this.AttemptPause);
}
// Continue to make attempts until either the file is marked as available
// or the number of current attempts is the same as or greater than the
// AccessAttempts property.
} while (!this.isFileAvailable && this.AccessAttempts > tiAttemptNumber);
}
这是我正在运行的代码,你可以在private void AvailableCheck中看到,OnfileAvailable是一个名为passen this的委托和文件事件args。
现在我继承了这个抽象类,我需要能够捕获那个事件。
toWatcher.Created += new FileSystemEventHandler(OnCreated);
在主要调用,在代码中向下调用以下方法
private void OnCreated(object source, FileSystemEventArgs e)
{
lstStatus.Invoke(new MethodInvoker(delegate {
lstStatus.Items.Add(DateTime.Now.ToString("g") + " - " + e.Name + " - File Created Event Detected for: " + e.FullPath);
lstStatus.TopIndex = lstStatus.Items.Count - 1;
tgtFile ThisFile = new tgtFile(e.FullPath);
lstStatus.Items.Add(DateTime.Now.ToString("g") + " - " + e.Name + " - Creating tgtFile Object");
}));
}
tgtFile对象的instatiation传递路径,该路径沿着长矛向下移动到可用方法。
正如您所看到的,存在从tgtFile对象触发OnFileAvailable事件的可能性。
同样如你所见,基于filesystemwatcher的线程设计,同时存在多个tgtFile对象存在于内存中的可能性。
在我的主应用程序中,然后我希望能够做类似的事情:
public tgtFile ThisFile;
ThisFile.OnFileAvailable += new EventHandler(OnFileAvailable);
但是EventHandler出错了,这就是我被困住的地方。
答案 0 :(得分:2)
如果它给你一个编译器错误,可能是因为你在这行中引用的“OnFileAvailable”方法(来自帖子的底部):
ThisFile.OnFileAvailable += new EventHandler(OnFileAvailable);
不期待EventHandler
- 它期待FileAvailable
代表。将其更改为:
ThisFile.OnFileAvailable += new absFile.FileAvailable(OnFileAvailable);
//note that this can also just be ThisFile.OnFileAvailable += OnFileAvailable;
并确保OnFileAvailable如下所示:
public void OnFileAvailable(object sender, FileEventArgs e)
{
//...
}
答案 1 :(得分:0)
首先要做的事情......在调用之前确保事件已订阅。将事件调用包装在IF语句中:
if (OnFileAvailable != null)
OnFileAvailable(this, toFileEventArgs);