private void ComputeHashThread() { var threadId = Interlocked.Increment(ref activeComputeHashThreads); Console.WriteLine("HashComputer {0} has started", threadId); FileQueueItem item; byte[] hash; string note; var md5 = MD5.Create(); while (true) { lock (fileQueue.SyncRoot) { if (fileQueue.Count == 0) { if (fileQueue.IsCompleted) { break; } fileQueue.Wait(); } item = fileQueue.Dequeue(); } hash = null; note = ""; try { var stream = File.OpenRead(item.Item1); hash = md5.ComputeHash(stream); stream.Close(); } catch (Exception e) { note = e.Message; } lock (hashQueue.SyncRoot) { hashQueue.Enqueue(new DbQueueItem(item.Item1, hash, note)); hashQueue.Pulse(); } } var threadsRemaining = Interlocked.Decrement(ref activeComputeHashThreads); Console.WriteLine("HashComputer {0} has finished ({1} remaining)", threadId, threadsRemaining); if (threadsRemaining == 0) { lock (hashQueue.SyncRoot) { hashQueue.Complete(); hashQueue.PulseAll(); } } }
private void Iterator() { command.CommandText = "DROP TABLE IF EXISTS hashtable"; command.ExecuteNonQuery(); command.CommandText = "CREATE TABLE hashtable (id INT IDENTITY(1,1) PRIMARY KEY, path VARCHAR(512), hash BINARY(16), notes VARCHAR(768))"; command.ExecuteNonQuery(); command.CommandText = "INSERT INTO hashtable (path,hash,notes) VALUES (@path,@hash,@notes)"; command.Parameters.AddWithValue("@path", null); command.Parameters.AddWithValue("@hash", null); command.Parameters.AddWithValue("@notes", null); DbQueueItem item; while (true) { lock (hashQueue.SyncRoot) { if (hashQueue.Count == 0) { if (hashQueue.IsCompleted) { break; } hashQueue.Wait(); } item = hashQueue.Dequeue(); } command.Parameters[0].Value = item.Item1; command.Parameters[1].Value = item.Item2 ?? System.Data.SqlTypes.SqlBinary.Null; command.Parameters[2].Value = item.Item3; command.ExecuteNonQuery(); Console.WriteLine(item.ToString()); } conn.Close(); Console.WriteLine("DB Iterator has finished"); }