looking for a brokerage account or IRA... click here Add To Favorites
return to index 

C# Threading

Threading in C# can be a lot of fun. Below is the code for a program which will read domains from a file and ping them. If the ping fails, it writes the results out to a file.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;
using System.Net;
using System.Net.NetworkInformation;
using System.Threading;
namespace Click_Domain_Checker
{
    class Program
    {
        static StreamWriter w = new StreamWriter("c:\\domainsresults.txt");
        static Hashtable domains = new Hashtable();

        static void Main(string[] args)
        {
            FileStream fsr = new FileStream("c:\\domains.txt",FileMode.Open,FileAccess.Read);
            StreamReader r = new StreamReader(fsr);
            string line=null;
            string[] linePieces = new string[10];

            while ((line = r.ReadLine()) != null)
            {
                linePieces = line.Split(''\t'');
                domains.Add(linePieces[0].Trim(), 1);
            }

            int i = 0;
            foreach (DictionaryEntry dr in domains)
            {
                string domain = dr.Key.ToString();
                string prStatus = "";

                Thread t = new System.Threading.Thread(new ParameterizedThreadStart(PingDomain));
                t.Start(domain);
                i++;
                if (i % 300 == 1)
                    Thread.Sleep(5000);
             }



        }

        static void PingDomain(object domainobject)
        {
            string domain = (string)domainobject;
            Ping ping = new Ping();
            PingReply pr;
            string prStatus = "";
            try
            {

                pr = ping.Send(domain);
                prStatus = pr.Status.ToString().Trim();
                if (prStatus != "Success")
                {
                    lock (w)
                    {
                        w.WriteLine(domain);
                    }
                    Console.WriteLine(domain);                    
                }
            }
            catch
            {
                lock (w)
                {
                    w.WriteLine(domain);
                }
                Console.WriteLine(domain);
            }
        }
    }
}


Additional Interesting Articles

AOL Postmaster Errors
C# MailMessage Example
PHP Cookie And Authentication
Email Deliverability at AOL
Transactions Inside of SQL Stored Procedures
Common Phrases Used In SPAM

©2008 AndrewKimball.com