Console.WriteLine在C#Eventhandler中不起作用
一个简单的程序,当我尝试使用Console.WriteLine记录更改时,使用FileSystemWatcher轮询目录中的更改,但它不起作用Console.WriteLine在C#Eventhandler中不起作用,c#,console-application,C#,Console Application,一个简单的程序,当我尝试使用Console.WriteLine记录更改时,使用FileSystemWatcher轮询目录中的更改,但它不起作用 由于Console.WriteLine在任何线程中都能正常工作,因此不确定是什么导致了此问题您的程序在启动线程后立即退出。您需要保持程序运行。 一种简单的方法是包含一个Console.ReadLine来阻止程序退出 class Program { FileSystemWatcher _watchFolder; string sPath =
由于Console.WriteLine在任何线程中都能正常工作,因此不确定是什么导致了此问题您的程序在启动线程后立即退出。您需要保持程序运行。 一种简单的方法是包含一个Console.ReadLine来阻止程序退出
class Program
{
FileSystemWatcher _watchFolder;
string sPath = @"D:\TestMonitor";
static void Main(string[] args)
{
Program p = new Program();
Thread t = new Thread(new ThreadStart(p.startActivityMonitoring));
t.Start();
}
private void startActivityMonitoring()
{
_watchFolder = new FileSystemWatcher();
_watchFolder.Path = Convert.ToString(sPath);
_watchFolder.NotifyFilter = System.IO.NotifyFilters.DirectoryName;
_watchFolder.NotifyFilter =
_watchFolder.NotifyFilter | System.IO.NotifyFilters.FileName;
_watchFolder.NotifyFilter =
_watchFolder.NotifyFilter | System.IO.NotifyFilters.Attributes;
_watchFolder.Changed += new FileSystemEventHandler(eventRaised);
_watchFolder.Created += new FileSystemEventHandler(eventRaised);
_watchFolder.Deleted += new FileSystemEventHandler(eventRaised);
_watchFolder.Renamed += new System.IO.RenamedEventHandler(eventRaised);
_watchFolder.EnableRaisingEvents = true;
}
private void eventRaised(object sender, System.IO.FileSystemEventArgs e)
{
switch (e.ChangeType)
{
case WatcherChangeTypes.Changed:
Console.WriteLine(string.Format("File {0} has been modified\r\n", e.FullPath));
break;
case WatcherChangeTypes.Created:
Console.WriteLine(string.Format("File {0} has been created\r\n", e.FullPath));
break;
case WatcherChangeTypes.Deleted:
Console.WriteLine(string.Format("File {0} has been deleted\r\n", e.FullPath));
break;
default: // Another action
break;
}
}
}
程序启动线程后立即退出。您需要保持程序运行。 一种简单的方法是包含一个Console.ReadLine来阻止程序退出
class Program
{
FileSystemWatcher _watchFolder;
string sPath = @"D:\TestMonitor";
static void Main(string[] args)
{
Program p = new Program();
Thread t = new Thread(new ThreadStart(p.startActivityMonitoring));
t.Start();
}
private void startActivityMonitoring()
{
_watchFolder = new FileSystemWatcher();
_watchFolder.Path = Convert.ToString(sPath);
_watchFolder.NotifyFilter = System.IO.NotifyFilters.DirectoryName;
_watchFolder.NotifyFilter =
_watchFolder.NotifyFilter | System.IO.NotifyFilters.FileName;
_watchFolder.NotifyFilter =
_watchFolder.NotifyFilter | System.IO.NotifyFilters.Attributes;
_watchFolder.Changed += new FileSystemEventHandler(eventRaised);
_watchFolder.Created += new FileSystemEventHandler(eventRaised);
_watchFolder.Deleted += new FileSystemEventHandler(eventRaised);
_watchFolder.Renamed += new System.IO.RenamedEventHandler(eventRaised);
_watchFolder.EnableRaisingEvents = true;
}
private void eventRaised(object sender, System.IO.FileSystemEventArgs e)
{
switch (e.ChangeType)
{
case WatcherChangeTypes.Changed:
Console.WriteLine(string.Format("File {0} has been modified\r\n", e.FullPath));
break;
case WatcherChangeTypes.Created:
Console.WriteLine(string.Format("File {0} has been created\r\n", e.FullPath));
break;
case WatcherChangeTypes.Deleted:
Console.WriteLine(string.Format("File {0} has been deleted\r\n", e.FullPath));
break;
default: // Another action
break;
}
}
}
您确定已触发事件并且调用了
控制台.WriteLine
方法吗?您确定已触发事件并且调用了控制台.WriteLine
方法吗?很抱歉,复制代码时错过了Console.WriteLine():(.实际问题是尽管我能够看到Console.WriteLine未登录到控制台的事件您缺少Console.ReadLine,这将使程序等待?很抱歉,我在复制代码时错过了Console.WriteLine():(.实际的问题是,尽管我能够看到Console.WriteLine引发的事件,但没有登录到控制台您缺少Console.ReadLine,这将使程序等待?