Click here to Skip to main content
12,755,332 members (35,982 online)
Click here to Skip to main content

Stats

83.5K views
476 downloads
75 bookmarked
Posted 6 Sep 2004

Threading paradigms available under the .NET CLR, using C#

, 23 Dec 2004
This article discusses the various threading paradigms available under the .NET CLR, using C#.
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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Gary J. Kuehn
Engineer
United States United States
No Biography provided

You may also be interested in...

Pro
Pro
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170217.1 | Last Updated 23 Dec 2004
Article Copyright 2004 by Gary J. Kuehn
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid