我本来发了帖子http://topic.csdn.net/u/20110711/09/9f5bb296-2b43-4ed9-9170-15458e9a7a83.html
用C#写的服务,不知道为什么运行一段时间后自动停止了,我就写成winform,开着测试,结果报了
System.StackOverflowException在events = new AutoResetEvent(false);
我想知道是什么原因导致的呢?如果换成 线程里无限循环呢?
用timer 又是怎样的呢?
用C#写的服务,不知道为什么运行一段时间后自动停止了,我就写成winform,开着测试,结果报了
System.StackOverflowException在events = new AutoResetEvent(false);
我想知道是什么原因导致的呢?如果换成 线程里无限循环呢?
用timer 又是怎样的呢?
events = new AutoResetEvent(false);
每次都new ,不释放吗?
假设你在一个方法里,使用类似public void test()
{
.....
ThreadPool.QueueUserWorkItem(h=> { test(); }
};这种代码,这并不会造成递归。只有直接调用test()才会递归。
private System.Threading.Thread timerlisten;
private static Hashtable fileInfos;
private static Hashtable traverse;
private Hashtable tempFiles;
protected void start()
{
fileInfos = new Hashtable();
traverse = new Hashtable();
tempFiles = new Hashtable();
timerlisten = new System.Threading.Thread(
new System.Threading.ThreadStart(monitor));
timerlisten.Start(); FileSystemWatcher curWatcher = new FileSystemWatcher(); curWatcher.BeginInit();
curWatcher.IncludeSubdirectories = true;
curWatcher.Path = System.Configuration.ConfigurationSettings.AppSettings["FileMonitorDirectory"];
//curWatcher.Changed += new FileSystemEventHandler(OnFileChanged);
curWatcher.Created += new FileSystemEventHandler(OnFileCreated);
//curWatcher.Deleted += new FileSystemEventHandler(OnFileDeleted);
//curWatcher.Renamed += new RenamedEventHandler(OnFileRenamed);
curWatcher.EnableRaisingEvents = true;
curWatcher.EndInit();
}
private void OnFileCreated(Object source, FileSystemEventArgs e)
{
if (servicePaused == false)
{
if (!timerlisten.IsAlive)
{
//当有文件创建时,如果检测线程没有运行,那么开启
timerlisten.Start();
}
lock (fileInfos)
{
FileInfo myfile;
try
{
myfile = new FileInfo(e.FullPath);
}
catch
{
//遇到所有异常此次获取文件信息操作均停止
return;
}
string filter = System.Configuration.ConfigurationSettings.AppSettings["filter"];
if (myfile.FullName.IndexOf(filter) == -1)
{
//如果上传的文件不是有upload标识的,不加入hashtable中
return;
}
string fileLength;
try
{
fileLength = myfile.Length.ToString();
}
catch (IOException)
{
return;
} middleHandle(true, myfile.FullName, fileLength); }
}
}
AutoResetEvent events;
protected void monitor()
{ events = new AutoResetEvent(false);
WaitCallback wait = new WaitCallback(startagain);
try
{
ThreadPool.QueueUserWorkItem(wait, events);
}
catch
{
return;
}
if (events.WaitOne(-1, true))
{ //Thread.Sleep(1000);
//events.Close();
//monitor();
}
} //中间处理,hashtable新添加值与监控线程处理hashtable必须互斥
private void middleHandle(bool flag, string name, string length)
{
lock (this)
{
if (true == flag)
{
tempFiles[name] = length; }
else
{
IDictionaryEnumerator idE = tempFiles.GetEnumerator(); while (idE.MoveNext())
{
fileInfos[idE.Key] = idE.Value;
traverse[idE.Key] = idE.Value;
}
tempFiles.Clear();
}
}
} void startagain(object obj)
{
if (servicePaused == false)
{
while (true)
{
if (traverse.Count == 0)
{
System.Threading.Thread.Sleep(1000);
middleHandle(false, "", "");
} IDictionaryEnumerator idE = fileInfos.GetEnumerator();
try
{
while (idE.MoveNext())
{
string oldFile;
string currentFileLength;
FileInfo currentFile; try
{
if (!traverse.ContainsKey(idE.Key))
{
//如果此键已被处理,则跳出此次操作
continue;
}
else
{
oldFile = traverse[idE.Key].ToString();
}
}
catch (ArgumentNullException)
{
continue;
}
System.Threading.Thread.Sleep(2500);
try
{
currentFile = new FileInfo(idE.Key.ToString());
currentFileLength = currentFile.Length.ToString();
}
catch (FileNotFoundException)
{
traverse.Remove(idE.Key);
continue;
} if (oldFile == currentFileLength)
{
try
{
currentFile.MoveTo(currentFile.FullName.Replace("upload", ""));
}
catch
{
//意外获取到2次的大小都一样,但是文件还是没传输完,捕获此异常,跳出循环体
continue;
}
traverse.Remove(idE.Key); }
else
{
traverse[idE.Key] = currentFile.Length.ToString(); }
if (traverse.Count == 0)
{
try
{
fileInfos.Clear();
}
catch
{
break;
}
break;
}
}
}
catch (InvalidOperationException)
{
//枚举操作无效时,直接进入下次循环
continue;
} }//end while
}
}这是我现在在运行的...用了个while(true)
不用循环的话用timer?
还是靠什么来触发