Click here to Skip to main content
15,895,011 members
Articles / High Performance Computing / Parallel Processing

Distributed and Parallel Processing using WCF

Rate me:
Please Sign up or sign in to vote.
4.24/5 (9 votes)
19 Apr 2009CPOL7 min read 108.3K   1.9K   41  
How to provide parallel and distributed computing capabilities using WCF
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Threading;

namespace Master
{
    /// <summary>
    /// This is the job distributer.
    /// </summary>
    public class JobDistributer
    {
        /// <summary>
        /// This is the proxy to the client.
        /// </summary>
        ServiceReference.JobPerformerClient jobPerformerClient
            = new global::Master.ServiceReference.JobPerformerClient();
 
        /// <summary>
        /// This is the number of active threads in the system.
        /// </summary>
        private int threadCount = 0;

        /// <summary>
        /// This is the number of active threads in the system.
        /// </summary>
        public int ThreadCount
        {
            get
            {
                return threadCount;
            }

        }

        /// <summary>
        /// This tells if an error has occured in the processing.
        /// </summary>
        int hasErrorOccured = 0;

        public int HasErrorOccured
        {
            get
            {
                return hasErrorOccured;
            }
        }

        /// <summary>
        /// The status of no error occured
        /// </summary>
        int statusNoError = 0;

        /// <summary>
        /// This is the init where the async registration takes place.
        /// </summary>
        public JobDistributer()
        {
            jobPerformerClient.SaveEntriesCompleted += new EventHandler<global::Master.ServiceReference.SaveEntriesCompletedEventArgs>(jobPerformerClient_SaveEntriesCompleted);
        }

        /// <summary>
        /// This method is called when execution is completed.
        /// </summary>
        /// <param name="sender">This is the sender</param>
        /// <param name="e">This is the result</param>
        void jobPerformerClient_SaveEntriesCompleted(object sender, global::Master.ServiceReference.SaveEntriesCompletedEventArgs e)
        {
            //decrement the thread count
            Interlocked.Decrement(ref threadCount);
            //if an error has not occured
            if (hasErrorOccured == 0)
            {
                //if an error took place then set the value as error occured.
                if (e.Result == false)
                {
                    Interlocked.Increment(ref hasErrorOccured);
                }
            }
        }

        /// <summary>
        /// This method is called to execute request.
        /// </summary>
        /// <param name="fileId">The Id of the file.</param>
        /// <param name="message">The message to be sent.</param>
        /// <param name="count">The count.</param>
        public void RequestExecution(int fileId,string message,int count)
        {
            //Increase the thread count
            Interlocked.Increment(ref threadCount);
            //call to save the message
            jobPerformerClient.SaveEntriesAsync(fileId, message, count);
        }
    }
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Imfinity
India India
Hi I have been working on enterprise applications for last six years.

Comments and Discussions