Click here to Skip to main content
15,885,757 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 100K   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.Diagnostics;

using System.Runtime.Remoting.Messaging;

namespace ManagedThreadCS_Async
{
 public delegate void ThreadDelegate();
	public class CGSRMgrThread
	{
  private IAsyncResult     _ar;
  private ManualResetEvent _mre;
  private ArrayList        _vecGSR;
  private String           _name;
	 private int              _vec;


  public CGSRMgrThread(int vec)
  {
   _vec = vec;
		   
   _vecGSR = new ArrayList();
   _mre = new ManualResetEvent(false);

   ThreadDelegate td = new ThreadDelegate(this.ThreadFunc);
   _ar = td.BeginInvoke(new AsyncCallback(this.ThreadFuncCallback), null);
  }
 
  
  ///////////////////////////////////////////////////////////////////////////////
  //  Function    : ThreadFunc
  //  Description : This function contains the code to be executed by the
  //                secondary thread.
  //                The signature of this function must be the same as the 
  //                WaitCallback delegate
  //
  // Modification History
  //  May xx, 2004  gjk  initial design
  //  May 25, 2004  gjk  Eliminated the subdivision of the obligor list.
  //                     Added Event object (set event on thread completion)
  ///////////////////////////////////////////////////////////////////////////////
  //
  public void ThreadFunc()
	  {
       Thread.Sleep(5);
       _name = Thread.CurrentThread.GetHashCode().ToString();
       Console.WriteLine("Thread {0} has initialized",_name);
       if (_vec != 0)
        {
         GetGSRs(_vec);
         Console.WriteLine("Thread {0} Intermediate Result : {1}", _name, _vec);
        }
       Console.WriteLine("Number of records saved : {0}", _vecGSR.Count);
       Console.WriteLine("Thread {0} finished", _name);

       // signal event indicating completion of the thread
       _mre.Set();
	  }

  ///////////////////////////////////////////////////////////////////////////////
  //  Function    : ThreadFuncCallback
  //  Description : This function is used to complete the asynch thread by
  //                calling EndInvoke on the delegate object.
  //
  // Modification History
  //  Jun 02, 2004  gjk  initial design
  ///////////////////////////////////////////////////////////////////////////////
  //
  public void ThreadFuncCallback(IAsyncResult ar)
  {
   AsyncResult aResult = (AsyncResult)ar;
   ThreadDelegate temp = (ThreadDelegate)aResult.AsyncDelegate;
   temp.EndInvoke(ar);
  }

  ///////////////////////////////////////////////////////////////////////////////
  //  Function    : GetMRE
  //  Description : Returns the event object for this instance
  //
  // Modification History
  //  May 25, 2004  gjk  original
  ///////////////////////////////////////////////////////////////////////////////
  //
  public ManualResetEvent GetMRE() {return _mre;}

 
  ///////////////////////////////////////////////////////////////////////////////
  //  Function    : GetAR
  //  Description : Returns the asynch result object for this instance
  //
  // Modification History
  //  Jun 02, 2004  gjk  original
  ///////////////////////////////////////////////////////////////////////////////
  //
  public IAsyncResult GetAR() {return _ar;}


	 public void GetGSRs(int obligor)
	  {
    // simulating the retrieval of a number of records for the specified obligor
    // get records

	   // get a record count which is a multiple of the obligor # for testing
    int num = obligor * 2 ;
    for (Int32 x=0; x < num; x++)
     {
      String s;
    	 s = "GSR";
      s = String.Concat(s, x.ToString());
      CGSR gsr = new CGSR(s,"USD",0,0,x);
      gsr.SetObligor(obligor);
      _vecGSR.Add(gsr);
     }
	  }

	 public ArrayList GetGSRs() {return _vecGSR;}
	 protected CGSRMgrThread(){;}

	}
}

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