Click here to Skip to main content
15,893,814 members
Articles / Programming Languages / C#

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

Rate me:
Please Sign up or sign in to vote.
4.76/5 (14 votes)
22 Dec 20047 min read 101K   633   75  
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 class CGSRMgr
 {
  private ArrayList    _vecObligor;
  private ArrayList    _vecThread;


  public CGSRMgr()
  {
   // create vector for complete obligor list
   _vecObligor = new ArrayList();

   // create vector which will hold each thread reference
   _vecThread = new ArrayList();
  }


  ///////////////////////////////////////////////////////////////////////////////
  //  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
     // func goes here.
     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);

    // wait for all threads to complete
    foreach (CGSRMgrThread t in _vecThread)
      t.GetMRE().WaitOne();
  }


  ///////////////////////////////////////////////////////////////////////////////
  //  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


Written By
Engineer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions