我有一个控制台应用程序不使用的代码终止保持控制台应用程序从关闭但仍运行代码
new System.Threading.AutoResetEvent(false).WaitOne();
我想达到的目标:我想运行一个StopWatch
,如果它符合它会运行某些文件操作代码的条件。然后最后在代码块后,重置计时器并等待它重新运行再次true
。
问题:然而,调试后我无法让我的代码通过我的条件,即使它已经通过了所需的条件。
我的代码:
static void Main(string[] args)
{
string mutex_id = "41585f436f766572743243494d";
using (System.Threading.Mutex mtx = new System.Threading.Mutex(false, mutex_id))
{
if(!mtx.WaitOne(0,false))
{
return;
}
processTimer = new Stopwatch();
processTimer.Start();
if (processTimer.Elapsed.Seconds > 10)
{
processTimer.Stop();
fileQueue = Directory.GetFiles(ConfigurationManager.AppSettings["WatchPath"], ConfigurationManager.AppSettings["Format"]).ToList();
}
//process the fileQueue
//..
//..
//processTimer.Reset(); -> Reset Timer to wait for another 10 sec and process again
new System.Threading.AutoResetEvent(false).WaitOne();
}
}
我从前使用过FileSystemWatcher
,但我没能得到正确的过程(如同连续/并行文件创建和这样)。尝试Threading
和Timers
作为我的question。
现在我试图从新的角度来处理这个问题。希望有些人能够用这个来启发我。
为什么要使用['StopWatch'](https://msdn.microsoft.com/en-us/library/ system.diagnostics.stopwatch(v = vs.110).aspx),而不是说[[Timer]](https://msdn.microsoft.com/en-us/library/system.threading.timer(v = vs.110)的.aspx)? –