首先,代码:
lblFileNbr.Text = "?/?";
lblFileNbr.ToolTipText = "Searching for files...";
lock(_fileLock)
{
_dirFiles = new string[0];
_fileIndex = 0;
}
if(_fileThread != null && _fileThread.IsAlive)
{
_fileThread.Abort();
}
_fileThread = new Thread(() =>
{
string dir = Path.GetDirectoryName(fileName) ?? ".";
lock (_fileLock)
{
_dirFiles = GetImageFileExtensions().SelectMany(f => Directory.GetFiles(dir, f, _searchOption)).OrderBy(f => f).ToArray();
_fileIndex = Array.IndexOf(_dirFiles, fileName);
}
int totalFileCount = Directory.GetFiles(dir, "*.*", _searchOption).Length;
Invoke((MethodInvoker)delegate
{
lblFileNbr.Text = string.Format("{0}/{1}", NumberFormat(_fileIndex + 1), NumberFormat(_dirFiles.Length));
lblFileNbr.ToolTipText = string.Format("{0} ({1} files ignored)", dir, NumberFormat(totalFileCount - _dirFiles.Length));
});
});
_fileThread.Start();
我正在构建一个小的图像查看程序。打开图像时,它会列出同一目录中的文件数。我注意到,当我在包含许多其他文件(比如 150K)的目录中打开图像时,构建文件列表需要几秒钟。因此,我将此任务委托(delegate)给另一个线程。
但是,如果您在完成搜索文件之前打开另一个图像,则旧计数不再相关,因此我将中止线程。
我正在锁定
_dirFiles
和 _fileIndex
因为我想添加一些左右键功能来在照片之间切换,所以我必须在其他地方访问它们(但在 UI 线程中)。这安全吗?现在 C# 中处理线程的方法似乎有几十种,我只是想要一些简单的方法。
fileName
是一个局部变量(这意味着它将被“复制”到匿名函数中,对吗?),和 _searchOption
是只读的,所以我想这两个可以安全访问。
最佳答案
> Is it safe to abort this file-searching thread?
简短的回答是不!
中止线程几乎是不安全的,当您可能正在执行 native 代码时,此建议更适用。
如果您不能以足够快的速度合作退出(因为这是您对
Directory.GetFiles
的调用需要时间),您最好的选择是放弃线程:让它干净地完成但忽略其结果。一如既往,我推荐阅读 Joe Albahari's free ebook
关于winforms - 中止这个文件搜索线程是否安全?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10970684/