Reputation: 9286
The below code falls over with the error "The process cannot access the file because it is being used by another process". Im stumped as to whats wrong. Im running visual studio as administrator and non of the files are open in notepad .
private void Load_Click(object sender, RoutedEventArgs e)
{
if (txtInput.Text.Length > 1) {
//var rootDir = System.IO.Directory.GetCurrentDirectory();
string rootDir = @"C:\b";
string search = txtInput.Text.Replace(" ", "");
List<Thread> searches = new List<Thread>();
foreach (var file in new DirectoryInfo(rootDir).GetFiles().Where(z => z.LastWriteTime > DateTime.Now.AddDays(-7))) {
if (file.ToString().Contains(".log")) {
searches.Add(new Thread(new ThreadStart(() => AddDropdownItem(file.ToString(),search))));
}
}
//Run ten threads at a time and wait for them to finish
for (int i = 0; i < searches.Count; i = i + 10) {
List<Thread> pool = new List<Thread>();
for (int j = 0; j < 10; j++) {
if (i + j < searches.Count) {
Thread t = searches[(i + j)];
pool.Add(t);
}
}
foreach (Thread t in pool) {
t.Start();
}
foreach (Thread t in pool) {
t.Join();
}
}
}
}
private void AddDropdownItem(string file, string search)
{
if (GetFileContent(file.ToString()).Contains(search)) {
ComboBoxItem item = new ComboBoxItem();
item.Content = file.ToString();
Dispatcher.BeginInvoke(new ThreadStart(() => ddFiles.Items.Add(item)));
}
}
private string GetFileContent(string file)
{
string path = System.IO.Path.Combine(@"C:\b", file);
using (FileStream fs = new FileStream(path, FileMode.Open)) {
return new StreamReader(fs).ReadToEnd();
}
}
Upvotes: 3
Views: 283
Reputation: 48949
The problem is most likely with the way you are capturing the loop variable in the lambda expression. Remember, closures capture the variable, not the value. So basically the AddDropdownItem
method may be receiving a different value for the parameter file
than what you think it is. This is a well known behavioral caveat with closing over the loop variable.
Change the loop so that you copy the loop variable into a separate reference.
foreach (var file in new DirectoryInfo(rootDir).GetFiles().Where(z => z.LastWriteTime > DateTime.Now.AddDays(-7)))
{
if (file.ToString().Contains(".log"))
{
var capture = file;
searches.Add(new Thread(new ThreadStart(() => AddDropdownItem(capture.ToString(),search))));
}
}
I noticed a potential unrelated problem. It appears that you are creating a ComboBoxItem
from one of your worker threads. I do see that you are marshaling add operation onto the UI thread. I would make sure that the ComboBoxItem
is also created on the UI thread for good measure. They way you have it probably will not cause any issues, but I would play it safe. I tend to interrupt the rule of no UI element access from a thread other than the UI thread to its ultimate extreme.
Upvotes: 3
Reputation: 2568
I can't see the 'AddDropDownItem' but I'll bet you're opening the file in there and not closing the files when the thread is done with them. Deallocating the variables(or just letting them fall out of scope and letting the GC handle it) isn't enough. Explicitly close the files first before the thread finishes.
Upvotes: 1