我用C#制作了一个Windows服务,现在把它变成了Windows窗体。 (不要担心我做了必要的修改以使其工作)
所以现在突然间一些功能无法正常工作。表单不会给我一个错误,但它不会做它应该做的事情。这是一些相关代码:
private void Start_Click(object sender, EventArgs e)
{
this.fileSystemWatcher1 = new System.IO.FileSystemWatcher();
((System.ComponentModel.ISupportInitialize)(this.fileSystemWatcher1)).BeginInit();
fileSystemWatcher1.Path = source;
fileSystemWatcher1.Created += new FileSystemEventHandler(fileSystemWatcher1_Created);
fileSystemWatcher1.Changed += new FileSystemEventHandler(fileSystemWatcher1_Changed);
fileSystemWatcher1.Deleted += new FileSystemEventHandler(fileSystemWatcher1_Deleted);
fileSystemWatcher1.Renamed += new RenamedEventHandler(fileSystemWatcher1_Renamed);
this.fileSystemWatcher1.EnableRaisingEvents = true;
this.fileSystemWatcher1.IncludeSubdirectories = true;
((System.ComponentModel.ISupportInitialize)(this.fileSystemWatcher1)).EndInit();
logger("Service started " + DateTime.Now);
}
public static void logger(String entry)
{
String logfile = ConfigurationManager.AppSettings[@"log"];
if (File.Exists(@logfile))
{
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@logfile, true))
{
file.WriteLine(entry);
}
}
else
{
string[] lines = { entry };
System.IO.File.WriteAllLines(@logfile, lines);
}
}
//some more functions as the logger.
private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)
{
cut_copy = ConfigurationManager.AppSettings[@"cutter"];
logger("File created> " + e.FullPath + " -Date:" + DateTime.Now);
filepath = Path.Combine(source, e.Name);
name = Path.GetFileNameWithoutExtension(filepath);
extension = Path.GetExtension(e.FullPath);
size = e.Name.Length;
strSelectCmd = "INSERT INTO" + tablepostgresql + " (" + column1 + "," + column2 + "," + column3 + "," + column4 + ") VALUES('" + name + "','" + size + "',now(),'" + extension + "')";
readquery = "select * from " + tablemysql + " where name='" + name + "'";
Mysql();
postgresql();
Record();
if (string.IsNullOrEmpty(filename) == false)
{
if (Directory.Exists(e.FullPath))
{
copyfolder();
Directory.CreateDirectory(target);
}
else
{
if (WaitForFileAvailable(e.FullPath, TimeSpan.FromSeconds(30)))
{
var file = Path.Combine(source, e.Name);
var copy_file = Path.Combine(target, e.Name);
var destination = Path.Combine(target, Path.ChangeExtension(source, Path.GetExtension(source)));
if (File.Exists(file))// Check to see if the file exists.
{ //If it does delete the file in the target and copy the one from the source to the target.
File.Delete(copy_file);
}
File.Copy(e.FullPath, copy_file);
}
else // The file failed to become available within 10 seconds.
{
logger("Copy has failed reason: File is being used by another program");
}
}
}
else
{
query = "INSERT INTO " + tablemysql + " (name,size,last_edit,extension) VALUES('" + name + "','" + size + "',now(),'" + extension + "')";
Mysql();
}
variable_reset();
}
我的问题是,按钮中的所有功能都正常工作,例如logger("Service started " + DateTime.Now);
,但fileSystemWatcher1_Created
不起作用(它什么都不做)。
这可能只是一个非常愚蠢的问题,但我很困惑,自从我为此工作以来已经很长时间了。
答案 0 :(得分:1)
将SynchronizingObject
的属性FileSystemWatcher
设置为您的表单:
this.fileSystemWatcher1 = new System.IO.FileSystemWatcher();
this.fileSystemWatcher1.SynchronizingObject = this;
FileSystemWatcher.SynchronizingObject Property@MSDN
处理更改,创建,删除和重命名事件时 一个可视的Windows窗体组件,如Button,访问 通过系统线程池的组件可能不起作用,或者可能导致 例外。通过将SynchronizingObject设置为a来避免这种情况 Windows窗体组件,它导致处理该方法的方法 已更改,创建,删除和重命名要在其上调用的事件 创建组件的线程。
附注:属性SynchronizingObject
为ISynchronizeInvoke
,Control
也为ISynchronizeInvoke
。