|
using System;
using System.Threading;
using System.Collections;
using System.IO;
namespace ManagedThreadCS
{
public delegate void CGSRMgrEventHandler(object sender, EventArgs e);
public class CGSRMgr
{
private ManualResetEvent _mre;
private Thread _threadMonitor;
private ArrayList _vecObligor;
private ArrayList _vecThread;
public event CGSRMgrEventHandler Notify;
public CGSRMgr()
{
// create vector for complete obligor list
_vecObligor = new ArrayList();
// create vector which will hold each thread reference
_vecThread = new ArrayList();
// create thread for monitoring any and all secondary queued threads
_threadMonitor = new Thread(new ThreadStart(this.funcThreadMonitor));
// reset event used for tracking the secondary threads
_mre = new ManualResetEvent(false);
}
///////////////////////////////////////////////////////////////////////////////
// Function : OnCGSRMgr
// Description : This function raises an event to all wired in delegates.
// The purpose of the event is to indicate that the mgr
// has completed all assigned tasks.
//
// Modification History
// Jun 12, 2004 gjk original
///////////////////////////////////////////////////////////////////////////////
//
protected virtual void OnCGSRMgr(EventArgs e)
{
if (Notify != null)
Notify(this,e);
}
///////////////////////////////////////////////////////////////////////////////
// Function : GetMRE
// Description : Returns the event object for this instance
//
// Modification History
// Jun 11, 2004 gjk original
///////////////////////////////////////////////////////////////////////////////
//
public ManualResetEvent GetMRE() {return _mre;}
///////////////////////////////////////////////////////////////////////////////
// Function : funcThreadMonitor
// Description : This function watches all secondary threads for completion
//
// Modification History
// Jun 11, 2004 gjk initial design
///////////////////////////////////////////////////////////////////////////////
//
private void funcThreadMonitor()
{
// look at each queued thread in the vector of threads to see if they
// have completed. Upon completion the mgr will issue a notification
// message
// determine the total number of possible threads
Int32 totalCount = _vecThread.Count;
// conditional used to prevent the creation of a wait handle array
// if more than 64 (max system defined parameter) entries are to be made
if (totalCount < 64)
{
Console.WriteLine ("Thread Monitor started using wait array");
// construct wait handle array
Int32 x = 0;
WaitHandle[] waitHandle = new WaitHandle[totalCount];
foreach (CGSRMgrThread t in _vecThread)
{
waitHandle[x] = t.GetMRE();
++x;
}
// perform wait operation
WaitHandle.WaitAll(waitHandle);
}
else
{
Console.WriteLine ("Thread Monitor started using wait one");
// wait for all threads to complete
foreach (CGSRMgrThread t in _vecThread)
t.GetMRE().WaitOne();
}
// at this point we have accounted for all threads completing
// issue notification
OnCGSRMgr(EventArgs.Empty);
_mre.Set();
Console.WriteLine ("INTERNAL COMPLETION EVENT SET");
}
///////////////////////////////////////////////////////////////////////////////
// Function : ProcessObligorList()
// Description : This function creates a number of threads via the exposed
// delegate. Each thread will in turn operate upon its own
// set of integers.
//
// Modification History
// May xx, 2004 gjk initial design
// May 25, 2004 gjk Eliminated the subdivision of the obligor list.
// Utilize the CLR ThreadPool for all the worker threads
///////////////////////////////////////////////////////////////////////////////
//
public void ProcessObligorList()
{
// apportion data among worker threads
foreach(int x in _vecObligor)
{
// get obligor set for each thread
CGSRMgrThread t = new CGSRMgrThread(x);
ThreadPool.QueueUserWorkItem(new WaitCallback(t.ThreadFunc));
_vecThread.Add(t);
}
Console.WriteLine("Total number of calls made to ThreadPool = {0}", _vecThread.Count);
// start thread monitor
_threadMonitor.Start();
}
///////////////////////////////////////////////////////////////////////////////
// Function : StreamToFile()
// Description : This function currently stands as a debug function for
// streaming, to the file system, various details for all
// records under the manager's control.
//
// Modification History
// May xx, 2004 gjk initial design
///////////////////////////////////////////////////////////////////////////////
//
public void StreamToFile(String fname)
{
if (File.Exists(fname))
File.Delete(fname);
StreamWriter sw = File.CreateText(fname);
foreach (CGSRMgrThread t in _vecThread)
{
ArrayList x = t.GetGSRs();
foreach (CGSR g in x)
{
sw.WriteLine("{0} {1}", g.GetObligor(),g.GetSymbol());
}
}
sw.Close();
}
///////////////////////////////////////////////////////////////////////////////
// Function : GetTotalGSRCount()
// Description : This function determines the total number of records
// processed across all threads control by this manager object
//
// Modification History
// May xx, 2004 gjk initial design
///////////////////////////////////////////////////////////////////////////////
//
public int GetTotalGSRCount()
{
int total = 0;
foreach (CGSRMgrThread t in _vecThread)
{
total += t.GetGSRs().Count;
}
return total;
}
public void SetObligorList(ArrayList obList)
{
_vecObligor = obList;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.