pymatio 0 Light Poster

Hi everybody, I'm trying to make a program that goes through a directory and checks for broken links. Unfortunately it isn't working:

// Main.cs

using System;
using System.Collections.Generic;
using Gtk;

namespace LinkMonkey
{
	class MainClass
	{
		public static void Main (string[] args)
		{
			bool cli = true;
			bool recursive = true;
			string dir = ".";
			for(int i=0; i<args.Length; i++){
				string arg = args[i];
				if(arg == "-g" || arg == "--gui"){
					cli = false;
				}else if(arg == "-d" || arg == "--dir"){
					if(i > args.Length){
						Console.WriteLine("-d/--dir takes an argument");
						return;
					}
					dir = args[++i];
				}else if(arg == "-r")
					recursive = false;
			}
			if(!cli){
				Application.Init ();
				MainWindow win = new MainWindow ();
				win.Show ();
				Application.Run ();
			}else{
				LibLinkMonkey validator = new LibLinkMonkey(dir, recursive);
				validator.handleFiles();
			}
		}
	}
}
// Lib

using System;
using System.IO;
using System.Threading;
using System.Net.Sockets;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace LinkMonkey
{
	public class LibLinkMonkey
	{
		private static object lockobj = new object ();
		private static List<string> files = new List<string> ();

		public LibLinkMonkey (string directory, bool recursive)
		{
			files = CheckDirectory (directory, recursive);
		}

		// get all files in a directory
		public List<string> CheckDirectory (string directory, bool recursive)
		{
			List<string> fileslist = new List<string> ();
			if (!Directory.Exists (directory))
				return fileslist;
			// otherwise
			string[] files = Directory.GetFiles (directory);
			string[] dirs = Directory.GetDirectories (directory);
			foreach (string file in files) {
				if (file.EndsWith (".html") || file.EndsWith (".htm"))
					fileslist.Add (file);
			}
			if (recursive) {
				foreach (string dir in dirs) {
					List<string> flisttmp = CheckDirectory (dir, true);
					foreach (string file in flisttmp)
						fileslist.Add (file);
				}
			}
			
			return fileslist;
		}

		private bool checkNet (string link)
		{
			Socket sock = new Socket (AddressFamily.Unspecified, SocketType.Stream, ProtocolType.Tcp);
			try {
				sock.Connect (link, 80);
				if (sock.Connected)
					return true;
				else
					return false;
				
			} catch (Exception ex) {
				return false;
			}
			
		}

		private bool checkLinks (string link)
		{
			bool works = false;
			if (link.StartsWith ("http") || link.StartsWith ("www."))
				works = checkNet (link);
			
			return works;
		}

		private fileData matchLinks (string data, fileData f)
		{
			string esp = "\"";
			string reg = String.Format ("href.*?\"(?<href>.*?){0}", esp);
			int linkc = 0;
			foreach (string line in data.Split ('\n')) {
				if (Regex.IsMatch (line, reg)) {
					MatchCollection mc = Regex.Matches (line, reg);
					foreach (Match match in mc) {
						string[] strip = match.ToString ().Split (new char[] { '\'', '"' });
						if (!checkLinks (strip[1]))
							f.addLink (strip[1]);
						linkc++;
					}
				}
			}
			f.linkc = linkc;
			return f;
		}

		private void handleFile (object file)
		{
			string filestr = (string)file;
			fileData fdata = new fileData (filestr);
			fdata = matchLinks (File.ReadAllText (filestr), fdata);
			fdata.write (lockobj);
		}

		public void handleFiles ()
		{
			WaitCallback callback = new WaitCallback (handleFile);
			foreach (string file in files){
				ThreadPool.QueueUserWorkItem (callback, file);
			}

			int mxthread, available, tmp;
			while (true) {
				ThreadPool.GetMaxThreads (out mxthread, out tmp);
				ThreadPool.GetAvailableThreads (out available, out tmp);
				if (available == mxthread)
					break;

				Thread.Sleep (100);
			}
		}
	}
}
// fileData.cs

using System;
using System.Collections.Generic;

namespace LinkMonkey
{
	public class fileData
	{
		public string filename;
		public int linkc = 0;
		List<string> links = new List<string>();
		public fileData(string __filename)
		{
			filename = __filename;
		}
		
		public void addLink(string link){
			links.Add(link);
		}
		
		public void write(object lockobj){
			lock(lockobj){
				Console.WriteLine("%% {0} %%", filename);
				if (linkc > 0){
					if(links.Count == 0)
						Console.WriteLine("All links work");
					else{
						Console.WriteLine("Links that don't work: ");
						foreach(string link in links)
							Console.WriteLine("  {0}", link);
					}
					Console.WriteLine("Number of links checked: {0}", linkc);
				}else
					Console.WriteLine("No links found");
				Console.WriteLine();
			}
		}
	}
}

When I run it on a directory with around 42 .htm(l) files it only outputs the data for around 12, I'm guessing this is something to do with threads.

Cans someone tell me where I'm going worng?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.