从服务到表单突然不起作用
本文关键字:突然 不起作用 表单 服务 | 更新日期: 2023-09-27 18:37:25
我用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
不起作用(它什么也不做)。这可能只是一个非常愚蠢的问题,但我非常困惑,我已经很久没有研究这个问题了。
将 FileSystemWatcher
的属性SynchronizingObject
设置为窗体:
this.fileSystemWatcher1 = new System.IO.FileSystemWatcher();
this.fileSystemWatcher1.SynchronizingObject = this;
文件系统观察者.同步对象Property@MSDN
当更改、创建、删除和重命名事件由 可视 Windows 窗体组件(如按钮),访问 通过系统线程池的组件可能无法工作,或者可能导致 在例外中。通过将同步对象设置为 Windows 窗体组件,它导致处理 更改、已创建、已删除和重命名的事件,以调用同一事件 在其上创建组件的线程。
旁注:属性SynchronizingObject
是ISynchronizeInvoke
,Control
也是ISynchronizeInvoke
。