I am using the following 2 methods. Method called DoMyWork1 does scale well like it takes 6 seconds to run three of them in 3 threads. Whereas DoMyJob method does not scale at all. If one thread takes 4 seconds then it takes 13 seconds to run 3 threads. What am I doing wrong? Does file read and/or write needs special thread handling other than thread pool?
My calling code
public static void Process(MyDelegate md , int threads)
{
int threadcount = threads;
ManualResetEvent[] doneEvents = new ManualResetEvent[threadcount];
DateTime dtstart = DateTime.Now;
List<string> myfiles = GetMyFiles(@"c:\");
for (int i = 0; i < threadcount; i++)
{
doneEvents[i] = new ManualResetEvent(false);
MyState ms = new MyState();
ms.ThreadIndex = i;
ms.EventDone = doneEvents[i];
ms.files = myfiles;
ThreadPool.QueueUserWorkItem(md.Invoke, ms);
}
WaitHandle.WaitAll(doneEvents);
DateTime dtend = DateTime.Now;
TimeSpan ts = dtend - dtstart;
Console.WriteLine("All complete in {0} seconds.", ts.ToString());
Console.ReadLine();
}
public static void DoMyWork1(Object threadContext)
{
MyState st = (MyState)threadContext;
Console.WriteLine("thread {0} started...", st.ThreadIndex);
Thread.Sleep(5000);
Console.WriteLine("thread {0} finished...", st.ThreadIndex);
st.EventDone.Set();
}
private static void DoMyJob(MyState st)
{
Console.WriteLine("I am in thread {0} started...", st.ThreadIndex);
string[] mystrings = new string[] { "one", "two", "three" };
foreach (string s in mystrings)
{
foreach (string file in st.files)
{
if (!(new StreamReader(file).ReadToEnd().Contains(s)))
{
AppendToFile(String.Format("{0} word searching in file {1} in thread {2}", s, file, st.ThreadIndex));
}
}
}
Console.WriteLine("I am in thread {0} ended...", st.ThreadIndex);
}